简体   繁体   English

Chart.js 图表格式 - 图例 & Y 轴 & 标题

[英]Chart.js Chart Formatting - Legend & Y Axis & Title

SOLVED解决了

Solution: Changed the spelling of axis to axes解决方案:将axis的拼写改为axes

I have just begun working with Chart.js as an alternative to the Python Plot.ly library.我刚刚开始使用Chart.js作为Python Plot.ly库的替代品。 So far I have been able to get the chart formatted the way I would like but there are still a couple of nuances I cant get right.到目前为止,我已经能够按照我想要的方式格式化图表,但仍有一些细微差别我无法正确处理。

The first of which is that I cannot get any solution for adding a $ before Y-Axis values.第一个是我无法获得在 Y 轴值之前添加$任何解决方案。 Here is my chart followed by the chart's code:这是我的图表,后跟图表代码: 在此处输入图片说明

<script>
    var ctx = document.getElementById("myChart").getContext('2d');
    var months = ['Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec'];
    var O1 = [1000,2000,3000,4000,5000,6000,7000,8000,9000,10000,11000,12000];
    var O2 = [3000,6000,5000,3000,8000,6000,4000,7000,8000,0000,3000,1000];

    var mychart = new Chart(ctx, {
        type: 'line',
        options: {
            title:{
                text: 'Test Chart',
                display: true,
                fontStyle: 'bold',
                fontSize: 16,
            },
            legend:{
                position: 'right',
            },
            scales:{
                yAxis:[{
                    scaleLabel:{
                        display: true,
                        labelString: 'Tester',
                    },
                    ticks:{
                        callback: function(value, index, values) {
                            if(parseInt(value) >= 1000){
                                return '$' + value.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",");
                            } else {
                                return '$' + value;
                            }
                        }
                    }
                }]
            },
            tooltips:{
                callbacks: {
                    label: function(tooltipItem, data){
                        var dataLabel = data.labels[tooltipItem.index];
                        var value = ': $ ' + data.datasets[tooltipItem.datasetIndex].data[tooltipItem.index].toLocaleString();
                        if (Chart.helpers.isArray(dataLabel)) {
                            dataLabel = dataLabel.slice();
                            dataLabel[0] += value;
                        } else {
                            dataLabel += value;
                        }
                        return dataLabel1;
                    }
                }
            }
        },
        data: {
            labels: months,
            datasets: [
                {
                label: 'Officer1',
                data: O1,
                fill: false,
                backgroundColor: 'rgba(255, 99, 132)',
                borderColor: 'rgba(255, 99, 132)',
                },
                {
                label: 'Officer2',
                data: O2,
                fill: false,
                backgroundColor: 'rgba(0,0,255)',
                borderColor: 'rgba(0,0,255)',
                }
            ]
        }
    });
</script>

As you may be able to see I would simply like to add a $ before my Y-Axis data values.正如您可能看到的,我只想在 Y 轴数据值之前添加一个$ The last solution I tried also broke the data up by 1000s.我尝试的最后一个解决方案也将数据分解了 1000 秒。

Additional:额外的:

The next few issues I've come across are very minor, which is why I chose to include them here.我遇到的接下来的几个问题非常小,这就是我选择将它们包含在此处的原因。

First I would like a way to center my title over the chart itself, rather than centered over the whole chart+legend item.首先,我想要一种将我的标题置于图表本身的中心,而不是以整个图表+图例项目为中心的方法。

Second is I would like to add some space to the left of my legend, as to separate it more from the chart.其次是我想在我的图例左侧添加一些空间,以便将它与图表分开。

Lastly, my Y-Axis title will not display.最后,我的 Y 轴标题不会显示。 I would like it to display here while im testing in case I need it in the future.我希望它在我测试时显示在这里,以防我将来需要它。 This issue was solved by changing the spelling of axis to axes ...subtle这个问题是通过将axis的拼写更改为axes ...subtle来解决的

axis的拼写更改为axes

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

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