[英]multiple line charts with independent data Javascript (Chart.js or google-vizualisation)
js and I have two datasets: js 和我有两个数据集:
data1 = [[0,1],[2,3],[5,7]] and data2 = [[1,4],[2,6],[5,2],[7,1]] for example. data1 = [[0,1],[2,3],[5,7]] 和 data2 = [[1,4],[2,6],[5,2],[7,1]]例子。 Each data list contains lists that represent points to plot on a same chart.
每个数据列表都包含表示同一图表上指向 plot 的点的列表。 (x and y values)
(x 和 y 值)
I want to plot exactely like this: https://www.chartjs.org/samples/latest/charts/line/multi-axis.html我想 plot 完全像这样: https://www.chartjs.org/samples/latest/charts/line/multi-axis.ZFC35FDC70D5FC69D269883A822C7A53E
But as you can see, my data lists don't have the same x or y values and they don't even have the same size, so I can't use the regular:但正如您所看到的,我的数据列表没有相同的 x 或 y 值,它们甚至没有相同的大小,所以我不能使用常规:
data: {labels = [1,2,3,4,5],
data = [7,8,3,1,2],
data = [9,1,2,3,4]} //for example
How can I code this chart only with javascript (no jQuery please)?如何仅使用 javascript 编码此图表(请不要使用 jQuery)? I didn't find anything on the Internet that might help.
我在互联网上没有找到任何可能有帮助的东西。 Any suggestions would matter to me !
任何建议对我都很重要!
You can use a scatter chart , that accepts the data
as an array of objects containing x
and y
properties.您可以使用散点图,它接受
data
作为包含x
和y
属性的对象数组。 To turn it into a line chart, define showLine: true
inside the data
configuration objects.要将其转换为折线图,请在
data
配置对象中定义showLine: true
。
Given your data structures, the following line of code produces the data structure expected by Chart.js.给定您的数据结构,以下代码行生成 Chart.js 预期的数据结构。
data1.map(o => ({ x: o[0], y: o[1] }))
Please have a look at below runnable code snippet.请查看下面的可运行代码片段。
const data1 = [[0,1],[2,3],[5,7]]; const data2 = [[1,4],[2,6],[5,2],[7,1]]; new Chart('line-chart', { type: "scatter", responsive: true, data: { datasets: [ { data: data1.map(o => ({ x: o[0], y: o[1] })), label: 'Dataset 1', showLine: true, fill: false, borderColor: 'red' }, { data: data2.map(o => ({ x: o[0], y: o[1] })), label: 'Dataset 2', showLine: true, fill: false, borderColor: 'blue' } ] }, options: { scales: { yAxes: [{ ticks: { beginAtZero: true } }] } } });
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.9.3/Chart.min.js"></script> <canvas id="line-chart" height="80"></canvas>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.