Last active
October 5, 2016 08:46
-
-
Save jannesaranpaa/d267758fee7a7f94a80ca7e573ecd11e to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| "use strict"; | |
| function explode_array(array) | |
| { | |
| let items = {}; | |
| } | |
| let data = [{"A": 1, "B": 2}, {"A": 2, "B": 3}]; | |
| console.log(explode_array(data)); | |
| function plot(data) | |
| { | |
| // Extract x and y points into two separate arrays for easy manipulation | |
| let points = {} | |
| points.x = []; | |
| points.y = []; | |
| for (item of data) | |
| { | |
| points.x.push(item.x); | |
| points.y.push(item.y); | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class Chart | |
| { | |
| constructor(graph, data) | |
| { | |
| this.graph = graph; | |
| this.data = data; | |
| } | |
| plot() | |
| { | |
| let dates = []; | |
| let values = []; | |
| for (let item of this.data) | |
| { | |
| dates.push(item.date); | |
| values.push(item.value); | |
| } | |
| let max_date = Math.max(...dates); | |
| let min_date = Math.min(...dates); | |
| let d_date = max_date - min_date; | |
| let max_value = Math.max(...values); | |
| let min_value = Math.min(...values); | |
| let d_value = max_value - min_value; | |
| // console.log(min_date); | |
| // console.log(max_date); | |
| // console.log(d_date); | |
| let positions = []; | |
| for (let item of this.data) | |
| { | |
| let x = 10 + Math.abs(((max_date - item.date) - d_date) / d_date) * 480; | |
| let y = 270 - (20 + Math.abs(((max_value - item.value) - d_value) / d_value) * 230); | |
| positions.push({"x": x, "y": y}); | |
| } | |
| // console.log(positions); | |
| let path = ""; | |
| let first_time = true; | |
| for (let position of positions) | |
| { | |
| path += (first_time ? "M " : " L ") + position.x + " " + position.y; | |
| if (first_time) {first_time = false;} | |
| } | |
| // console.log(path); | |
| this.graph.find(".line > path").attr("d", path); | |
| this.graph.find(".area-plot > path").attr("d", | |
| "M 0 270 V " + positions[0].y + " H 10 " + | |
| path.replace("M", "L") + | |
| " H 500 V 270 Z"); | |
| } | |
| } | |
| let generated = [{"date": 1000, "value": 8}]; | |
| for (let i = 1; i < 101; i++) | |
| { | |
| let prev_date = generated[i-1].date; | |
| let date = prev_date + Math.floor(Math.random() * (60000 - 1000)) + 1000; | |
| let prev_value = generated[i-1].value; | |
| let value = (Math.random() * ((prev_value + 1) - (prev_value - 1))) + (prev_value - 1); | |
| // console.log(new Date(date), value); | |
| generated.push({"date": date, "value": value}); | |
| } | |
| for (item of generated) | |
| { | |
| item.date = new Date(item.date); | |
| } | |
| console.log(generated); | |
| let sanoma_chart = new Chart($("svg.line-chart#sanoma"), generated) | |
| sanoma_chart.plot(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment