首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >显示未定义的Chart.js图例文本

显示未定义的Chart.js图例文本
EN

Stack Overflow用户
提问于 2018-05-04 16:29:33
回答 2查看 17.2K关注 0票数 10

我试图在chart.js中显示四个带有图例的水平条;但图例文本在每个案例中都显示为undefined。如何在chart.js中定义或声明图例的文本?

我的html代码:

代码语言:javascript
复制
    <div class="chart bar-chart dark">
        <h3 class="title">Name</h3>
        <p class="tagline">chocolates</p>
        <canvas height="400" id="barChartHDark"></canvas>
    </div>
    <div class="chart radar-chart light">
        <h3 class="title">Bauxite</h3>
        <p class="tagline">P&B Dispatch</p>
        <canvas height="400" id="radarChartLight"></canvas>
    </div>

我的javascript代码:

代码语言:javascript
复制
Charts.prototype.initBarHorizontal = function () {
    var ctxD = $("#barChartHDark"), chartData = {
        type: 'horizontalBar',
        data: {
            labels: ["Today", "Last week", "Last month", "Last Year"],
            datasets: [{
                    data: [7, 59, 68, 26],
                    backgroundColor: this.colors[0],
                    hoverBackgroundColor: this.convertHex(this.colors[0], 70),
                },
                {
                    data: [, 23, 44, 30],
                    backgroundColor: this.colors[1],
                    hoverBackgroundColor: this.convertHex(this.colors[1], 70),
},
                {
                    data: [, 3, -7, 1],
                    backgroundColor: this.colors[2],
                    hoverBackgroundColor: this.convertHex(this.colors[2], 70),
                }]
        },
        options: {
            barThickness: 1,
            scales: {
                xAxes: [{
                        stacked: true,
                        ticks: {
                            fontColor: this.tickColor
                        },
                        gridLines: {
                            drawOnChartArea: false
                        }
                    }],
                yAxes: [{
                        stacked: true,
                        ticks: {
                            fontColor: this.tickColor,
                            min: 0,
                            max: 175,
                            stepSize: 25
                        }
                    }]
            },
            labels: ['Current data','Vs last week/month/year','Change'],
            name: ['Current data','Vs last week/month/year','Change'],
            legend: {
                display: true,
                legendText : ['Current','Vs last week/month/year','% Change']
                    },     
        }
    }, myDarkRadarChart = new Chart(ctxD, chartData), myLightRadarChart = new Chart(ctxL, chartData);
};

它给出的输出如下:

如何更改顶部显示为“未定义”的文本?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2018-05-04 16:33:03

试试这个:)

代码语言:javascript
复制
   Charts.prototype.initBarHorizontal = function () {
    var ctxD = $("#barChartHDark"), chartData = {
    type: 'horizontalBar',
    data: {
        labels: ["Today", "Last week", "Last month", "Last Year"],
        datasets: [{
                label: 'Something1',
                data: [7, 59, 68, 26],
                backgroundColor: this.colors[0],
                hoverBackgroundColor: this.convertHex(this.colors[0], 70),
            },
            {
                label: 'Something2',
                data: [, 23, 44, 30],
                backgroundColor: this.colors[1],
                hoverBackgroundColor: this.convertHex(this.colors[1], 70),
},
            {
                label: 'Something3',
                data: [, 3, -7, 1],
                backgroundColor: this.colors[2],
                hoverBackgroundColor: this.convertHex(this.colors[2], 70),
            }]
    },
    options: {
        barThickness: 1,
        scales: {
            xAxes: [{
                    stacked: true,
                    ticks: {
                        fontColor: this.tickColor
                    },
                    gridLines: {
                        drawOnChartArea: false
                    }
                }],
            yAxes: [{
                    stacked: true,
                    ticks: {
                        fontColor: this.tickColor,
                        min: 0,
                        max: 175,
                        stepSize: 25
                    }
                }]
        },
        labels: ['Current data','Vs last week/month/year','Change'],
        name: ['Current data','Vs last week/month/year','Change'],
        legend: {
            display: true,
            legendText : ['Current','Vs last week/month/year','% Change']
                },     
    }
}, myDarkRadarChart = new Chart(ctxD, chartData), myLightRadarChart = new Chart(ctxL, chartData);
};

对于每个数据集,您必须添加:

代码语言:javascript
复制
label: 'Something'
票数 11
EN

Stack Overflow用户

发布于 2018-05-04 16:36:47

您可以尝试使用文档中定义的来定义标签。https://www.chartjs.org/docs/latest/configuration/legend.html#configuration-options。我相信它是标签(作为图例标签项的数组)和

{text: "something"}

作为一种模式(参见https://www.chartjs.org/docs/latest/configuration/legend.html#legend-item-interface)。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50170521

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档