BarChart的附加行(Charts.js)

时间:2015-01-06 16:57:51

标签: javascript html5 canvas html5-canvas chart.js

我正在尝试通过下面的代码生成的条形图添加水平线(例如计算的平均值)。有没有办法用另一个画布覆盖该条形图?或者我是否必须更改“Chart.js”文件以获得这样的一行?如果有必要,必须改变什么?

<!doctype html>
<html>
  <head>
    <script src="Chart.js"></script>
  </head>
  <body>
    <center>
    <h2>
    <u>Statistik</u>
    </h2>
    <canvas id="canvas" height="450" width="600"></canvas>

    <script>
        var jan = 12
        var feb = 23;
        var mar = 11;
        var apr = 65;
        var mai = 33;
        var jun = 1;
        var jul = 23;
        var aug = 0;
        var sep =0;
        var okt = 0;
        var nov =0;
        var dec = 0;

        var barChartData = {
            labels : ["Januar","Februar","März","April","Mai","Juni","Juli", "August", "September", "Oktober", "November", "Dezember"],
            datasets : [
            {
                label : "2014",
                fillColor: "rgba(220,220,220,0.5)",
                strokeColor: "rgba(220,220,220,0.8)",
                highlightFill: "rgba(220,220,220,0.75)",
                highlightStroke: "rgba(220,220,220,1)",
                data : [12,0,0,0,0,0,0,0,0,0,0,0]
            }, 
            {
                label : "2015",
                fillColor : "rgba(151,187,205,0.5)",
                strokeColor : "rgba(151,187,205,0.8)",
                highlightFill : "rgba(151,187,205,0.75)",
                highlightStroke : "rgba(151,187,205,1)",
                data : [jan,feb,mar,apr,mai,jun,jul,aug,sep,okt,nov,dec]
            }]
        }
        var ctx = document.getElementById("canvas").getContext("2d");
        var chart = new Chart(ctx).Bar(barChartData, {
            responsive : true
        });
    </script>
  </body>
</html>

感谢您的帮助!

亲切的问候 vonGiso

1 个答案:

答案 0 :(得分:0)

我已经创建了一个称为叠加图表的东西,我已经添加到我的fork.js(https://github.com/leighquince/Chart.js)的分支中,可以在这种情况下使用。它的作用与行或条形图相同,唯一的区别是您声明了一个名为type的额外属性,可以是'line''bar'。然后只需致电new Chart(ctx).Overlay(data)

UPDATE:也添加到fork现在可以选择传递一个名为 stacked overlayBars的选项(更改了堆叠的名称,因为它有点误导它正在做的事情)会将条形图叠加在彼此前面。

&#13;
&#13;
var data = {
  labels: ["January", "February", "March", "April", "May", "June", "July"],
  datasets: [{
    label: "My First dataset",
    //new option, barline will default to bar as that what is used to create the scale
    type: "line",
    fillColor: "rgba(120,120,120,0.2)",
    strokeColor: "rgba(120,120,120,1)",
    pointColor: "rgba(120,120,120,1)",
    pointStrokeColor: "#fff",
    pointHighlightFill: "#fff",
    pointHighlightStroke: "rgba(220,220,220,1)",
    data: [65, 59, 4, 81, 56, 55, 40]
  }, {
    label: "My First dataset",
    //new option, barline will default to bar as that what is used to create the scale
    type: "bar",
    fillColor: "rgba(220,20,220,1)",
    strokeColor: "rgba(220,20,220,1)",
    pointColor: "rgba(220,20,220,1)",
    pointStrokeColor: "#fff",
    pointHighlightFill: "#fff",
    pointHighlightStroke: "rgba(220,220,220,1)",
    data: [32, 25, 33, 88, 12, 92, 33]
  }, {
    label: "My Second dataset",
    //new option, barline will default to bar as that what is used to create the scale
    type: "bar",
    fillColor: "rgba(30,30,130,1)",
    strokeColor: "rgba(30,30,130,1)",
    pointColor: "rgba(30,30,130,1)",
    pointStrokeColor: "#fff",

    data: [13, 29, 22, 44, 34, 77, 10]
  }]
};
var ctx = document.getElementById("canvas").getContext("2d");
var chart = new Chart(ctx).Overlay(data, {
  responsive: false,
  overlayBars: true,
  datasetFill: false,
});
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="http://www.quincewebdesign.com/cdn/Chart.js"></script>

<canvas id="canvas" width="400"></canvas>
&#13;
&#13;
&#13;