2016-09-13 87 views
2

I'm tryng使在chart.js之柱状图(使用chart.js之2.2.2)添加数据集条形图 - chart.js之

I'm麻烦试图把新的数据集在图表中

我如何可以把一个新的数据集“Vendas”的数据:[10,20,30,40,50,60,70]

var data = { 
     labels: ["January", "February", "March", "April", "May", "June", "July"], 
     datasets: [ 
      { 
       label: "Compras", 
       backgroundColor: [ 
        'rgba(255, 99, 132, 0.2)', 
        'rgba(54, 162, 235, 0.2)', 
        'rgba(255, 206, 86, 0.2)', 
        'rgba(75, 192, 192, 0.2)', 
        'rgba(153, 102, 255, 0.2)', 
        'rgba(255, 159, 64, 0.2)' 
       ], 
       borderColor: [ 
        'rgba(255,99,132,1)', 
        'rgba(54, 162, 235, 1)', 
        'rgba(255, 206, 86, 1)', 
        'rgba(75, 192, 192, 1)', 
        'rgba(153, 102, 255, 1)', 
        'rgba(255, 159, 64, 1)' 
       ], 
       borderWidth: 1, 
       data: [65, 59, 80, 81, 56, 55, 40], 
      } 
     ] 
    }; 
var ctx = $("#barOrgaoAno").get(0).getContext("2d"); 
     var myBarChart = new Chart(ctx,{ 
      type: "bar", 
      data: data, 
}); 

我试过两个例子我得到了在互联网,但我不能不能工作

例1

barChartDemo.addData([dData()], "dD " + index); 

Exemple2

JSFLiddle

var myNewDataset = { 
     label: "My Second dataset", 
     fillColor: "rgba(187,205,151,0.5)", 
     strokeColor: "rgba(187,205,151,0.8)", 
     highlightFill: "rgba(187,205,151,0.75)", 
     highlightStroke: "rgba(187,205,151,1)", 
     data: [48, 40, 19, 86, 27, 90, 28] 
    } 

    var bars = [] 
    myNewDataset.data.forEach(function (value, i) { 
     bars.push(new myBarChart.BarClass({ 
      value: value, 
      label: myBarChart.datasets[0].bars[i].label, 
      x: myBarChart.scale.calculateBarX(myBarChart.datasets.length + 1, myBarChart.datasets.length, i), 
      y: myBarChart.scale.endPoint, 
      width: myBarChart.scale.calculateBarWidth(myBarChart.datasets.length + 1), 
      base: myBarChart.scale.endPoint, 
      strokeColor: myNewDataset.strokeColor, 
      fillColor: myNewDataset.fillColor 
     })) 
    }) 

    myBarChart.datasets.push({ 
     bars: bars 
    }) 

    myBarChart.update(); 
+0

是否要包括Vendas年初设定或之后增加吗? –

+0

1月份会有2个酒吧,一个是“Compras”,另一个是“Vendas”等等,每个月都是如此,不管它是在 – dpanegassi

回答

5

既然你存储在一个变量(在你的代码中调用data)的图表数据,你可以用一个按钮一个简单的功能做到这一点:

$('button').click(function() { 
    // You create the new dataset `Vendas` with new data and color to differentiate 
    var newDataset = { 
     label: "Vendas", 
     backgroundColor: 'rgba(99, 255, 132, 0.2)', 
     borderColor: 'rgba(99, 255, 132, 1)', 
     borderWidth: 1, 
     data: [10, 20, 30, 40, 50, 60, 70], 
    } 

    // You add the newly created dataset to the list of `data` 
    data.datasets.push(newDataset); 

    // You update the chart to take into account the new dataset 
    myBarChart.update(); 
}); 


你可以看到完整的代码 on this jsFiddle和这里是它的结果在点击后:

enter image description here

+1

thx很多你的时间,像一个魅力工作 – dpanegassi

0

只是注意,你说你正在使用2.2.2,但你的小提琴包括1.0.2。

最起码你只需要这在开始添加到数据集的数组:如果你只需要Compras和Vendas

{ 
    label: "Vendas", 
    data: [10, 20, 30, 40, 50, 60, 70] 
} 

的话,那么整个事情看起来像:

var data = { 
    labels: ["January", "February", "March", "April", "May", "June", "July"], 
    datasets: [{ 
     label: "Compras", 
     data: [65, 59, 80, 81, 56, 55, 40] 
    }, { 
     label: "Vendas", 
     data: [10, 20, 30, 40, 50, 60, 70] 
    } 

    ] 
}; 

var ctx = document.getElementById("chart"); 

var myBarChart = new Chart(ctx, { 
    type: 'bar', 
    data: data, 
    options: { 
     scales: { 
      yAxes: [{ 
       ticks: { 
        beginAtZero:true 
       } 
      }] 
     } 
    } 
}); 

这意味着你可以从你的小提琴中摆脱“我的第二个数据集”和超时功能。

+0

之前还是之后加上,如果我还不清楚的话,可以输入 例1和2我在互联网上找到了 如果我想动态地放置它,就像按一个按钮并显示新的数据集? – dpanegassi