条形图顶部中间的折线图点

亚历克斯·曼

我已经创建了一个使用chartjs的组合双条形图。我的代码如下。条形组合二重图工作正常,但我要求为绿色条形图添加一条线,以连接所有顶部中点。我已经绘制了一些连接绿色图形的折线图,但是我面临的问题是,折线图点不在绿色条形图的顶部中间,如下所示。

谁能告诉我如何在条形图的顶部中间找到线条

![在此处输入图片描述

工作演示

html

<canvas id="canvas"></canvas>

js

var barChartData = {
        labels: ["January", "February", "March", "April", "May", "June", "July"],
        datasets: [{
            type: 'bar',
              label: "Visitor",
                data: [10, 59, 340, 521, 450, 200, 195],
                fill: false,
                backgroundColor: "rgba(220,220,220,0.5)",
                borderColor: '#71B37C',
                hoverBackgroundColor: '#71B37C',
                hoverBorderColor: '#71B37C'

        }, {
            type: 'bar',
              label: "Visitor",
                data: [200, 185, 590, 621, 250, 400, 95],
                fill: false,
                backgroundColor: '#71B37C',
                borderColor: '#71B37C',
                hoverBackgroundColor: '#71B37C',
                hoverBorderColor: '#71B37C'

        }, {

                type:'line',
                data: [200, 185, 590, 621, 250, 400, 95],
                fill: false,
                borderColor: '#EC932F',
                backgroundColor: '#EC932F',
                pointBorderColor: '#EC932F',
                pointBackgroundColor: '#EC932F',
                pointHoverBackgroundColor: '#EC932F',
                pointHoverBorderColor: '#EC932F'

        } ]
    };

    window.onload = function() {
        var ctx = document.getElementById("canvas").getContext("2d");
        window.myBar = new Chart(ctx, {
            type: 'bar',
            data: barChartData,
            options: {
            responsive: true,
            tooltips: {
              mode: 'label'
          },
          elements: {
            line: {
                fill: false
            }
        },
          scales: {
            xAxes: [{
                display: true,
                gridLines: {
                    display: false
                },
                labels: {
                    show: true,
                }
            }],
            yAxes: [{
                type: "linear",
                display: true,
                position: "left",
                id: "y-axis-1",
                gridLines:{
                    display: false
                },
                labels: {
                    show:true,

                }
            }, {
                type: "linear",
                display: true,
                position: "right",
                id: "y-axis-2",
                gridLines:{
                    display: false
                },
                labels: {
                    show:true,

                }
            }]
        }
        }
        });
    };
泰迪

要更改特定图表的显示,您必须编辑整个图表的配置。

在内部,您会发现需要更改特定图表(在您的情况下为折线图)所需的属性。


如果您尝试在其中进行深入搜索,最终将找到 折线图的 x y 属性,并将其存储在中 myBar.config.data.datasets[2].metaData[i]._model 我承认很难找到 )。

然后,您只需要将此代码添加到您的代码中(生成之后myBar):

// For each value of your line chart ...
for (var i = 0; i < myBar.config.data.datasets[2].metaData.length; i++) {
    // Get the bar width associated to this value  
    var barWidth = myBar.config.data.datasets[1].metaData[i]._model.width;
    // Get the percentage that the bar is taking in the graph
    var barPercentage = myBar.config.options.scales.xAxes[0].barPercentage;

    // Add the width of the bar / (2*percentage) -- which is the half of the bar
    myBar.config.data.datasets[2].metaData[i]._model.x += barWidth / (2*barPercentage);
    // Also edit the controlPointNext and controlPointPrevious to change the bezier curve display
    myBar.config.data.datasets[2].metaData[i]._model.controlPointNextX += barWidth / (2*barPercentage);
    myBar.config.data.datasets[2].metaData[i]._model.controlPointPreviousX += barWidth / (2*barPercentage);
}

检查插栓的完整代码。


这是最终结果: 在此处输入图片说明

更新-添加了响应能力:

为了使图具有响应性,您将需要在Chart.js插件内部实现循环

插件使您能够处理在创建,更新,渲染图形时触发的所有事件。

我们将特别编辑该afterUpdate事件,该事件在每次更新时都会触发(例如,调整大小)。

Chart.pluginService.register({
    afterUpdate: function(chart) {
        // Loop in here
    }
});

这是最终结果的另一个小提琴,它具有响应性。

请注意,条形图是在第一行之后绘制的(我不知道为什么),因此我不得不降低alpha条形背景色的。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

从数据框在条形图的顶部创建折线图时出错

如何从折线图更改为条形图

堆叠条形图+折线图角度库

将组合的条形图与截断的折线图组合

在JavaScript中组合条形图和折线图

分组条形图和折线图

ggplot组合的条形图和折线图

Highcharts获得组合的条形图和折线图

创建组合条形图和折线图,其值为“0”,从垂直线的中间开始 GOOGLE CHARTS

组合的条形图和折线图JfreeChart。试图使折线图平均3点。值排列不正确

如何在chart.js中的条形图和折线图顶部显示数据值3

尝试绘制条形图和折线图时的 gnuplot '无有效点'消息

无法在D3条形图/折线图中显示折线

情节:如何将条形图和折线图与条形图组合为子图?

使用pandas和matplotlib绘制叠加图(折线图和条形图)

无法使用pandas plot()函数组合条形图和折线图

D3.js组合条形图和折线图

在 Python pandas 中对齐折线图和条形图

如何用条形图绘制累积百分比折线图?

使用iOS图表的组合图表(折线图和条形图)

ggplot - 一个图上的双折线图和堆积条形图

Flot:在一张图表中组合堆积的条形图和折线图

堆积的条形图和折线图与任何android库在同一图中?

为什么Matplotlib开始绘制条形图而不是折线图

在ggplot2中组合条形图和折线图(双轴)

如何使用ggplot在一个图表中创建条形图和折线图

如何在 R 中使用折线图实现堆积条形图

Chart.js如何获取组合条形图和折线图?

如何在ggplot2中绘制组合的条形图和折线图