简体   繁体   English

ChartJS:如何使用水平准则制作条形图:

[英]ChartJS: how to make a bar chart with a horizontal guideline:

Is it possible to make a chart like the example below? 是否可以像下面的示例一样制作图表?

使用带有水平规则的chartjs的条形图

I haven't been able to find an example with an overlay. 我一直找不到覆盖的示例。 I'm using this in the context of an atmosphere plugin within a meteor application so it might not be possible for me to easily add plugins to my chartjs instance. 我在流星应用程序中的大气插件上下文中使用此功能,因此可能无法轻松将插件添加到我的chartjs实例。

I've built out quite a few charts already, so for that reason it's my strong preference to get this to work over switching to another charting library, but it's not too late to port them over if there's no other way to make this work. 我已经建立了很多图表,因此出于这个原因,我非常希望让它能够工作而不是切换到另一个图表库,但是如果没有其他方法可以进行移植,现在还不算太晚。

You could extend the Bar Chart to add the horizontal line 您可以扩展条形图以添加水平线

Chart.types.Bar.extend({
        name: "BarLine",
        draw: function () {

            Chart.types.Bar.prototype.draw.apply(this, arguments);

            var scale = this.scale,
                barHeight = this.scale.calculateY(83);


            // draw line
            this.chart.ctx.beginPath();
            this.chart.ctx.moveTo(30, barHeight);

            this.chart.ctx.strokeStyle = '#ff0000';
            this.chart.ctx.lineWidth = 3;
            this.chart.ctx.lineTo(this.chart.width, barHeight);
            this.chart.ctx.stroke();

            // write Label Text
            this.chart.ctx.fillStyle = '#000000';
            var text = this.options.labelText ? this.options.labelText : "DEFAULT TEXT" 
            this.chart.ctx.textAlign = 'center';
            this.chart.ctx.font = '22px Arial';
            this.chart.ctx.fillText(text, this.chart.width  * 0.5, 95);
            this.chart.ctx.closePath();                            

        }
    });`

You could add the bar values in the draw function i'm sure, but I generally do it in the 'onanimationcomplete' function for the chart, like so: 我可以确定,可以在绘制函数中添加条形值,但通常是在图表的“ onanimationcomplete”函数中添加它,如下所示:

resultChart = new Chart(resultGraphCanvas.getContext("2d")).BarLine(chart, {

                        scaleBeginAtZero: true,
                        scaleOverride: true,
                        scaleSteps: 10,
                        scaleStepWidth: 10,
                        maintainAspectRatio: false,
                        labelText: "TEST DRAWN NEAR THE LINE",

                        showTooltips: false, //Needs to be set to false for the onAnimationComplete drawing to persist
                        onAnimationComplete: function () {


                            var ctx = this.chart.ctx;
                            ctx.font = this.scale.font;
                            ctx.fillStyle = this.scale.textColor
                            ctx.textAlign = "center";
                            ctx.textBaseline = "bottom";

                            this.datasets.forEach(function (dataset) {
                                dataset.bars.forEach(function (bar) {

                                    ctx.fillText(bar.value, bar.x, bar.y );
                                });
                            })
                        },
                    });

This will draw the values at the top of the bars, i'll leave positioning them elsewhere to you :) 这将在条形图的顶部绘制值,我将它们放在其他位置:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM