Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript Chart.JS JSON中具有未知行数的多行图表_Javascript_Json_Multidimensional Array_Chart.js - Fatal编程技术网

Javascript Chart.JS JSON中具有未知行数的多行图表

Javascript Chart.JS JSON中具有未知行数的多行图表,javascript,json,multidimensional-array,chart.js,Javascript,Json,Multidimensional Array,Chart.js,我尝试使用chart.js创建一个多折线图。我有一个来自数据库的json数据集。数据集的数量可以不同。下面是一个使用JSON数据的示例(x可以更多) [{"name":"name1","jan":4067.5,"feb":1647, "mrz":1375,"apr":10191,"mai":0,"jun":28679,"jul":59502}, {"name":"name2","jan":47548,"feb":63280.5, "mrz":51640.26,"apr":75029,"mai":

我尝试使用chart.js创建一个多折线图。我有一个来自数据库的json数据集。数据集的数量可以不同。下面是一个使用JSON数据的示例(x可以更多)

[{"name":"name1","jan":4067.5,"feb":1647,
"mrz":1375,"apr":10191,"mai":0,"jun":28679,"jul":59502},
{"name":"name2","jan":47548,"feb":63280.5,
"mrz":51640.26,"apr":75029,"mai":41137,"jun":89114.26,"jul":77332},
{"name":"name3","jan":38099,"feb":55023.5,
"mrz":62668,"apr":39482,"mai":44193.3,"jun":52826.5,"jul":77072},
{"name":"namex","jan":34930.5,"feb":36831.5,
"mrz":24391,"apr":35051,"mai":38038,"jun":12700,"jul":51080}]
我简化了这个例子,事实上它一直到12月

我试着做一个图表,每个名字都有一条线。X轴应为1月至12月,Y轴为销售


据我所知,您的数据集将得到动态更新

您只需更改数据集,然后调用
update()
函数

例如:

首先使用以下方法使用所有数据初始化图表:

var canvas = document.getElementById("barChart");
var ctx = canvas.getContext('2d');

var data = {
  labels: ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"],
  datasets: [{
      label: "name 1",
      fill: false,
      lineTension: 0.1,
      backgroundColor: "rgba(225,0,0,0.4)",
      borderColor: "red",
      borderCapStyle: 'square',
      borderDash: [],
      borderDashOffset: 0.0,
      borderJoinStyle: 'miter',
      pointBorderColor: "black",
      pointBackgroundColor: "white",
      pointBorderWidth: 1,
      pointHoverRadius: 8,
      pointHoverBackgroundColor: "yellow",
      pointHoverBorderColor: "brown",
      pointHoverBorderWidth: 2,
      pointRadius: 4,
      pointHitRadius: 10,
      data: [65, 59, 80, 81, 56, 55, 40, ,60,55,30,78]
    }, {
      label: "name 2",
      fill: true,
      lineTension: 0.1,
      backgroundColor: "rgba(167,105,0,0.4)",
      borderColor: "rgb(167, 105, 0)",
      borderCapStyle: 'butt',
      borderDash: [],
      borderDashOffset: 0.0,
      borderJoinStyle: 'miter',
      pointBorderColor: "white",
      pointBackgroundColor: "black",
      pointBorderWidth: 1,
      pointHoverRadius: 8,
      pointHoverBackgroundColor: "brown",
      pointHoverBorderColor: "yellow",
      pointHoverBorderWidth: 2,
      pointRadius: 4,
      pointHitRadius: 10,
      data: [10, 20, 60, 95, 64, 78, 90,,70,40,70,89]
    }

  ]
};

var options = {
  scales: {
            yAxes: [{
                ticks: {
                    beginAtZero:true
                },
                scaleLabel: {
                     display: true,
                     labelString: 'Moola',
                     fontSize: 20 
                  }
            }]            
        }  
};

// Chart declaration:
var myBarChart = new Chart(ctx, {
  type: 'line',
  data: data,
  options: options
});
然后,如果以后要更新数据,您可以:

myBarChart.data.datasets = [{
      label: "name 3",
      fill: true,
      lineTension: 0.1,
      backgroundColor: "rgba(167,105,0,0.4)",
      borderColor: "rgb(167, 105, 0)",
      borderCapStyle: 'butt',
      borderDash: [],
      borderDashOffset: 0.0,
      borderJoinStyle: 'miter',
      pointBorderColor: "white",
      pointBackgroundColor: "black",
      pointBorderWidth: 1,
      pointHoverRadius: 8,
      pointHoverBackgroundColor: "brown",
      pointHoverBorderColor: "yellow",
      pointHoverBorderWidth: 2,
      pointRadius: 4,
      pointHitRadius: 10,
      data: [10, 20, 60, 95, 64, 78, 90,,70,40,70,89]
    },
    ...............
];

myBarChart.update();
此外,您还需要以可与图表一起使用的方式格式化数据。
格式化数据,以便创建正确的数据集。

据我所知,您的数据集将得到动态更新

您只需更改数据集,然后调用
update()
函数

例如:

首先使用以下方法使用所有数据初始化图表:

var canvas = document.getElementById("barChart");
var ctx = canvas.getContext('2d');

var data = {
  labels: ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"],
  datasets: [{
      label: "name 1",
      fill: false,
      lineTension: 0.1,
      backgroundColor: "rgba(225,0,0,0.4)",
      borderColor: "red",
      borderCapStyle: 'square',
      borderDash: [],
      borderDashOffset: 0.0,
      borderJoinStyle: 'miter',
      pointBorderColor: "black",
      pointBackgroundColor: "white",
      pointBorderWidth: 1,
      pointHoverRadius: 8,
      pointHoverBackgroundColor: "yellow",
      pointHoverBorderColor: "brown",
      pointHoverBorderWidth: 2,
      pointRadius: 4,
      pointHitRadius: 10,
      data: [65, 59, 80, 81, 56, 55, 40, ,60,55,30,78]
    }, {
      label: "name 2",
      fill: true,
      lineTension: 0.1,
      backgroundColor: "rgba(167,105,0,0.4)",
      borderColor: "rgb(167, 105, 0)",
      borderCapStyle: 'butt',
      borderDash: [],
      borderDashOffset: 0.0,
      borderJoinStyle: 'miter',
      pointBorderColor: "white",
      pointBackgroundColor: "black",
      pointBorderWidth: 1,
      pointHoverRadius: 8,
      pointHoverBackgroundColor: "brown",
      pointHoverBorderColor: "yellow",
      pointHoverBorderWidth: 2,
      pointRadius: 4,
      pointHitRadius: 10,
      data: [10, 20, 60, 95, 64, 78, 90,,70,40,70,89]
    }

  ]
};

var options = {
  scales: {
            yAxes: [{
                ticks: {
                    beginAtZero:true
                },
                scaleLabel: {
                     display: true,
                     labelString: 'Moola',
                     fontSize: 20 
                  }
            }]            
        }  
};

// Chart declaration:
var myBarChart = new Chart(ctx, {
  type: 'line',
  data: data,
  options: options
});
然后,如果以后要更新数据,您可以:

myBarChart.data.datasets = [{
      label: "name 3",
      fill: true,
      lineTension: 0.1,
      backgroundColor: "rgba(167,105,0,0.4)",
      borderColor: "rgb(167, 105, 0)",
      borderCapStyle: 'butt',
      borderDash: [],
      borderDashOffset: 0.0,
      borderJoinStyle: 'miter',
      pointBorderColor: "white",
      pointBackgroundColor: "black",
      pointBorderWidth: 1,
      pointHoverRadius: 8,
      pointHoverBackgroundColor: "brown",
      pointHoverBorderColor: "yellow",
      pointHoverBorderWidth: 2,
      pointRadius: 4,
      pointHitRadius: 10,
      data: [10, 20, 60, 95, 64, 78, 90,,70,40,70,89]
    },
    ...............
];

myBarChart.update();
此外,您还需要以可与图表一起使用的方式格式化数据。
格式化数据,以便创建正确的数据集。

谢谢您的帮助,不,这不是它的本意。我有一个SQL查询,它可以包含5到10行。通话线路的数量是固定的。我想在图中为每个名称画一行,数据应该来自Json,并且在查询后固定。但它可能是明天,一个新的名字来了,但它是在JSON数据在其中。你能提供一些更多的细节。我不明白你想说什么。您的JSON数据将在一段时间后更新。对吗?对,我的JSON数据可以更改。名字是雇员。新的可以进来也可以离开我们。我想给每个员工打电话。我更改了JSON并在JSFIDLE上创建了数据。除了一个错误,我认为我非常接近解决方案:-)。在这里,我做了一些改变。我已经使用JSON数组创建了图表。这是链接。谢谢你的工作。它在JSFiddle中工作得非常好。但是在我自己的Web服务器上,我有以下错误消息:
Chart.bundle.js:6904 uncaughttypeerror:Object.acquireContext(Chart.bundle.js:6904)的Chart.construct(Chart.bundle.js:8423)的新图表(Chart.bundle.js:8410)的属性'length'为null在appdata16_1.js:60 acquireContext@Chart.bundle.js:6904 construct@Chart.bundle.js:8423 Chart@Chart.bundle.js:8410(匿名)@appdata16_1.js:60
感谢您的帮助,不,这不是它的本意。我有一个SQL查询,它可以包含5到10行。通话线路的数量是固定的。我想在图中为每个名称画一行,数据应该来自Json,并且在查询后固定。但它可能是明天,一个新的名字来了,但它是在JSON数据在其中。你能提供一些更多的细节。我不明白你想说什么。您的JSON数据将在一段时间后更新。对吗?对,我的JSON数据可以更改。名字是雇员。新的可以进来也可以离开我们。我想给每个员工打电话。我更改了JSON并在JSFIDLE上创建了数据。除了一个错误,我认为我非常接近解决方案:-)。在这里,我做了一些改变。我已经使用JSON数组创建了图表。这是链接。谢谢你的工作。它在JSFiddle中工作得非常好。但是在我自己的Web服务器上,我有以下错误消息:
Chart.bundle.js:6904 uncaughttypeerror:Object.acquireContext(Chart.bundle.js:6904)的Chart.construct(Chart.bundle.js:8423)的新图表(Chart.bundle.js:8410)的属性'length'为null在appdata16_1.js:60 acquiredcontext@Chart.bundle.js:6904 construct@Chart.bundle.js:8423 Chart@Chart.bundle.js:8410(匿名)@appdata16_1.js:60