更改chartjs/react-chart2js中折线图的最后一段的颜色。

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

Change the Colour of the last segment of line chart in chartjs/react-chart2js

问题

我可以为您提供关于如何访问数组的最后一个元素以更改其颜色的信息。您可以使用ctx.p1DataIndex 来访问数组的最后一个元素。在 last_element 函数中,您可以检查 ctx.p1DataIndex 是否等于数组的长度减去1,以确定当前元素是否为数组的最后一个元素。如果是最后一个元素,您可以返回您想要的颜色,否则返回undefined

以下是代码示例:

  1. const last_element = (ctx, value) => {
  2. if (ctx.p1DataIndex === historicalData.datasets[0].data.length - 1) {
  3. return value; // 返回您想要的颜色
  4. }
  5. return undefined;
  6. };

请确保在函数内访问 historicalData 对象以获取数据的长度。这样,您可以根据最后一个元素的条件来设置颜色。

英文:

I want to change the colour of last segment of a line chart in react, i am using chartjs library,

  1. import React from "react";
  2. import { Line } from "react-chartjs-2";
  3. import "../Styles/charitem.css";
  4. import {
  5. Chart,
  6. LinearScale,
  7. CategoryScale,
  8. PointElement,
  9. LineElement,
  10. } from "chart.js";
  11. Chart.register(LinearScale, CategoryScale, PointElement, LineElement);
  12. function ChartItem() {
  13. const last_element = (ctx, value) => {
  14. if (ctx.p0DataIndex === 0) {
  15. return value;
  16. }
  17. return undefined;
  18. };
  19. const historicalData = {
  20. labels: Array.from({ length: 7 }, (_, index) => getTimeLabel(index + 9, 0)),
  21. datasets: [
  22. {
  23. label: "Stock Price",
  24. data: Array.from({ length: 6 }, () => getRandomPrice(100, 250)),
  25. borderColor: "blue",
  26. backgroundColor: "rgba(0, 0, 255, 0.2)",
  27. tension: 0.4,
  28. segment: {
  29. borderColor: (ctx) =>
  30. last_element(ctx, "rgba(0, 0, 255, 0.2)") ||
  31. down(ctx, "rgba(255,0,0, 1)") ||
  32. " rgba(0,128,0,1)",
  33. },
  34. },
  35. ],
  36. };
  37. return (
  38. <div className="chartitem-div">
  39. <Line data={historicalData} options={options} />
  40. </div>
  41. );
  42. }
  43. // Helper function to generate random price between min and max
  44. function getRandomPrice(min, max) {
  45. return Math.floor(Math.random() * (max - min + 1)) + min;
  46. }
  47. // Helper function to format time label
  48. function getTimeLabel(hours, minutes) {
  49. return `${String(hours).padStart(2, "0")}:${String(minutes).padStart(
  50. 2,
  51. "0"
  52. )}`;
  53. }
  54. export default ChartItem;

I can do it for the first element using ctx.p0DataIndex in last_element function, how to access the last element of the array so that i can change its colour?

答案1

得分: 0

尽管我没有使用 react-chartjs-2 来完成这个任务,但我认为它应该对你有所帮助。

  1. const lastSegment = function(ctx, value){
  2. if(ctx.p1DataIndex == config.data.datasets[0].data.length-1){
  3. return value
  4. }
  5. }
  1. segment: {
  2. borderColor: ctx => lastSegment(ctx, '#FF0000')
  3. }

我将下一个点的索引与最后一个索引进行比较。如果两者相等,我会将线段的颜色更改为红色。

这里是完整的代码,以查看它的外观。

英文:

Even though I didn't use react-chartjs-2 to do this, I think it should help you.

  1. const lastSegment = function(ctx, value){
  2. if(ctx.p1DataIndex == config.data.datasets[0].data.length-1){
  3. return value
  4. }
  5. }
  1. segment: {
  2. borderColor: ctx => lastSegment(ctx, '#FF0000')
  3. }

I compare the index of the next point with the last index.
If the 2 are equal, I change the colour of the segment to red.

Here's the full code to see what it looks like

huangapple
  • 本文由 发表于 2023年7月27日 18:59:07
  • 转载请务必保留本文链接:https://go.coder-hub.com/76779063.html
匿名

发表评论

匿名网友

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

确定