ChartJS-线图 - 数据标签格式功能无法正常工作

发布于 2025-02-12 03:27:35 字数 1900 浏览 0 评论 0原文

我有以下html/js代码,如下所示:

  • 标签

并将其转换为带有标签上标签的线图:

<div style = "position:relative; bottom:140px; z-index: -2;">
<canvas id="lineChart" width = 600 height = 200></canvas>
<script>
    Chart.register(ChartDataLabels);
    var ctx = document.getElementById("lineChart").getContext("2d");
    var linechart = new Chart(ctx, {
        type: "line",
        data: {
            labels: {{ labels | safe}},
            datasets: [
                {
                    label: "Data points",
                    data: {{ values | safe}},
                    fill: false,
                    borderColor: "rgb(75, 192, 192)",
                    lineTension: 0.5
                }
            ]
        },
        options: {
        responsive: true,
        layout: {
            padding: {
                top: 100
                    }
                },
        scales: {
          x: {
            grid: {
              display: false
            }
          },
          y: {
            display: false,
          }
        },

        plugins: {
            legend: {
                display: false
            },
            datalabels: {
                anchor: 'end',
                align: 'top',
                formatter: Math.round,
                font: {
                    weight: 'bold',
                    size: 15
                }
            }

        }
        }
    });
</script>

这很好:

但是,我希望标签到达标签 时,请使用'£'符号,然后'm'

在每次尝试使用格式化函数

formatter: function(value){
            return value + 'm';

例如:或任何其他组合,图表都无法返回。我完全困惑为什么。

有什么建议吗?

I have the following HTML/JS code as follows which takes two lists:

  • labels
  • values

And converts it into a line chart with labels on the points:

<div style = "position:relative; bottom:140px; z-index: -2;">
<canvas id="lineChart" width = 600 height = 200></canvas>
<script>
    Chart.register(ChartDataLabels);
    var ctx = document.getElementById("lineChart").getContext("2d");
    var linechart = new Chart(ctx, {
        type: "line",
        data: {
            labels: {{ labels | safe}},
            datasets: [
                {
                    label: "Data points",
                    data: {{ values | safe}},
                    fill: false,
                    borderColor: "rgb(75, 192, 192)",
                    lineTension: 0.5
                }
            ]
        },
        options: {
        responsive: true,
        layout: {
            padding: {
                top: 100
                    }
                },
        scales: {
          x: {
            grid: {
              display: false
            }
          },
          y: {
            display: false,
          }
        },

        plugins: {
            legend: {
                display: false
            },
            datalabels: {
                anchor: 'end',
                align: 'top',
                formatter: Math.round,
                font: {
                    weight: 'bold',
                    size: 15
                }
            }

        }
        }
    });
</script>

This is working fine:

enter image description here

However, I want the labels to have the '£' symbol before and 'm' after

Every time I try to use the formatter function like:

formatter: function(value){
            return value + 'm';

Or any other combination, the chart fails to return. I am completely perplexed why.

Any suggestions?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

流年已逝 2025-02-19 03:27:35

以下格式对我来说很好。

formatter: v => '£' + v + 'm'

请在下面查看您的修订和可运行的代码。

Chart.register(ChartDataLabels);
new Chart('myChart', {
  type: "line",
  data: {
    labels: [2015, 2016, 2017, 2018, 2019, 2020, 2021],
    datasets: [{
      label: "Data points",
      data: [567, 894, 883, 833, 710, 714, 578],
      fill: false,
      borderColor: "rgb(75, 192, 192)",
      lineTension: 0.5
    }]
  },
  options: {
    responsive: true,
    layout: {
      padding: {
        top: 100
      }
    },
    scales: {
      x: {
        grid: {
          display: false
        }
      },
      y: {
        display: false,
      }
    },
    plugins: {
      legend: {
        display: false
      },
      datalabels: {
        anchor: 'end',
        align: 'top',
        formatter: v => '£' + v + 'm',
        font: {
          weight: 'bold',
          size: 15
        }
      }
    }
  }
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/3.8.0/chart.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/chartjs-plugin-datalabels@2"></script>
<canvas id="myChart" height="100"></canvas>

如果您的代码仍然不起作用,则某处可能存在语法错误。尝试打开浏览器的devtools,并检查“控制台”选项卡,以了解可帮助您找到问题源的任何错误。

The following formatter works fine for me.

formatter: v => '£' + v + 'm'

Please take a look at your amended and runnable code below.

Chart.register(ChartDataLabels);
new Chart('myChart', {
  type: "line",
  data: {
    labels: [2015, 2016, 2017, 2018, 2019, 2020, 2021],
    datasets: [{
      label: "Data points",
      data: [567, 894, 883, 833, 710, 714, 578],
      fill: false,
      borderColor: "rgb(75, 192, 192)",
      lineTension: 0.5
    }]
  },
  options: {
    responsive: true,
    layout: {
      padding: {
        top: 100
      }
    },
    scales: {
      x: {
        grid: {
          display: false
        }
      },
      y: {
        display: false,
      }
    },
    plugins: {
      legend: {
        display: false
      },
      datalabels: {
        anchor: 'end',
        align: 'top',
        formatter: v => '£' + v + 'm',
        font: {
          weight: 'bold',
          size: 15
        }
      }
    }
  }
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/3.8.0/chart.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/chartjs-plugin-datalabels@2"></script>
<canvas id="myChart" height="100"></canvas>

If your code still doesn't work, there may be a syntax error somewhere. Try to open your browser's DevTools and check the console tab for any errors that help you locate the source of the problem.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文