修改 chart.js 中的标签,当我更新图表时

huangapple go评论72阅读模式
英文:

Modify the labels in chart.js when I update the chart

问题

我正在使用chart.js创建一些动态图表。我知道如何创建不同类型的图表并修改要显示的数据,但问题是我不知道如何更新标签。

这里我只展示更新数据的代码:

const colores = ["#3e95cd", "#8e5ea2", "#3cba9f", "#e8c3b9", "#c45850", "#FFF850"];
const etiquetas = ["Africa", "Asia", "Europe", "Latin America", "North America", "Miguel"];
var paquetes_pie = null;

function graficoPaquetes(datos) {
    let ctx = document.getElementById("paquetes_graf").getContext("2d");
    let tipo = 'pie';
    let titulo = 'Paquetes perdidos';

    paquetes_pie = new Chart(ctx, {
    type: tipo,
    data: {
        labels: etiquetas,
        datasets: [{
            label: "Paquetes perdidos",
            backgroundColor: colores,
            borderWidth: 1,
            borderColor: '#FFF',
            data: datos,
        }]
    },
    options: {
        title: {
            display: true,
            text: titulo
        }
    }
  });
}

function addData(chart, title, data) {
  chart.options.title.text = title;
  chart.data.datasets.forEach((dataset, i) => {
    dataset.data = data;
  });
  chart.update();
}

$(document).ready(function() {
    
    var data = [2478, 5267, 734, 784, 433, 99];
    graficoPaquetes(data);

    $('.btn_graf').click(function() {
        data = [24, 67, 4, 4, 3, 9];
        addData(paquetes_pie, "Paquetes perdidos", data);
    });
});

现在我展示一些错误的标签更新想法:

function addData(chart, title, data, etiquetas) {
    chart.options.title.text = title;
    chart.data.datasets.forEach((dataset, i) => {
        dataset.data = data;
    });
    //chart.data.forEach((dat, i) => { //第一次尝试
        //dat.labels = etiquetas;
    //});
    //chart.data.labels = etiquetas; // 第二次尝试
    chart.update();
}

$('.btn_graf').click(function() {
    data = [24, 67, 4, 4, 3, 9];
    etiquetas = ["A", "B", "C", "D", "E", "F"];
    addData(paquetes_pie, "Paquetes perdidos", data, etiquetas);
});

希望这可以帮助你。

英文:

I'm working with chart.js to create some dynamical charts. I know how to create different kind of charts and modify the data that I want to show but the problem is that I don't know how to update the label.

Here I show my code just updating data:

const colores = ["#3e95cd", "#8e5ea2", "#3cba9f", "#e8c3b9", "#c45850", "#FFF850"];
const etiquetas = ["Africa", "Asia", "Europe", "Latin America", "North America","Miguel"];
var paquetes_pie = null;
function graficoPaquetes(datos) {
let ctx = document.getElementById("paquetes_graf").getContext("2d");
let tipo = 'pie';
let titulo = 'Paquetes perdidos';
paquetes_pie = new Chart(ctx, {
type: tipo,
data: {
labels: etiquetas,
datasets: [{
label: "Paquetes perdidos",
backgroundColor: colores,
borderWidth: 1,
borderColor: '#FFF',
data: datos,
}]
},
options: {
title: {
display: true,
text: titulo
}
}
});
}
function addData(chart, title, data) {
chart.options.title.text = title;
chart.data.datasets.forEach((dataset, i) => {
dataset.data = data;
});
chart.update();
}
$(document).ready(function() {
var data = [2478, 5267, 734, 784, 433, 99];
graficoPaquetes(data);
$('.btn_graf').click(function() {
data = [24, 67, 4, 4, 3, 9];
addData(paquetes_pie, "Paquetes perdidos", data);
});
});

Now I show some wrong ideas to update the label:

function addData(chart, title, data, etiquetas) {
chart.options.title.text = title;
chart.data.datasets.forEach((dataset, i) => {
dataset.data = data;
});
/*chart.data.forEach((dat, i) => { //first attempt
dat.labels = etiquetas;
});*/
//chart.data.labels = etiquetas; // Second attempt
chart.update();
}
$('.btn_graf').click(function() {
data = [24, 67, 4, 4, 3, 9];
etiquetas = ["A", "B", "C", "D", "E", "F"];
addData(paquetes_pie, "Paquetes perdidos", data, etiquetas);
});

Can somebody help me?

Thank you very much

答案1

得分: 3

你可以按如下方式更新:

chart.config.data.labels = newLabels // 你可以传递新的标签
chart.update()
英文:

You can update like as follows

chart.config.data.labels = newLabels // you can pass the new labels
chart.update()

答案2

得分: 0

尝试一下

chart.data.labels = 新标签
chart.update()
英文:

Try it also

chart.data.labels = newLabels
chart.update()

huangapple
  • 本文由 发表于 2020年1月6日 19:18:51
  • 转载请务必保留本文链接:https://go.coder-hub.com/59611169.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定