0

祝大家好日子:

最近我开始使用 Vue.js(2.6.12) + Vuetify(2.3.10) 和 Chart.js(3.0.2)。所以我是一个新手(再次)。

我创建了一个组件,它包装了 Chart.js 允许我们创建的条形图。这是它的图片:创建的组件

我要更改的唯一两件事是靠近图例标题的小框和一些网格 x 线。

在小图例框的情况下是红色的。我希望它与图例标题文本的蓝色对齐。正如我在这张照片中向您展示的那样:框和图例标题文本我想达到的结果

最后,我想在 X 轴上添加一些颜色(粉红色):

我无法做到这一点。我已经考虑并遵循了官方文档,但没有任何效果:S。

在图例标题框的情况下,我注意到它总是采用第一个元素的颜色。在轴的情况下,chart.js 支持的功能对我不起作用。它根本不打印 x 轴。

我尝试过的事情:

  • 升级到chart.js 3.3.0 但我收到这样的错误:“”我使用3.0.2 的原因是因为它是3.0.0 版本中唯一适用于我的版本。
  • 降级到 2.9.3/4。我无法更改框或轴线的颜色,但其余的工作正常。
  • 使用包装器:https ://vue-chartjs.org/ 。它没有用

整个组件的代码:

<template>
  <div class="container pa-3" fill-height fluid style="width: 100%">
    <!-- We create the chart -->
    <canvas id="myChart1" />
  </div>
</template>

<script>
import Chart from "chart.js/auto";

export default {
  name: "Chart",

  components: {},

  props: {},

  data: () => ({
    ctx: null,
    myChart: null,
    type: "bar",
    data: {
      labels: ["a", "b", "c", "d"],
      datasets: [
        {
          data: [1, 2, 3, 4],
          backgroundColor: ["#c30", "#e37609", "#ffda05", "#fffb05"],
        },
      ],
    },
    options: {
      plugins: {
        legend: {
          display: true,
          labels: {
            color: "#00a3fb",
          },
        },
      },
      scales: {
   
      },
    },
  }),
  methods: {
    createChart: function () {
      // destroy the previous graph
      if (this.myChart != null) this.myChart.destroy();
      // create a new one
      this.ctx = document.getElementById("myChart1");
      this.myChart = new Chart(this.ctx, {
        type: this.type,
        data: this.data,
        options: this.options,
      });
      this.myChart.render();
    },
  },

  destroyed() {},

  mounted() {
    this.createChart();
  },
  watch: {},
};
</script>

<style scoped>
</style>

为了使用它,您应该:

  • 在部分中导入它
  • 在组件部分声明它
  • <NameOfComponetGiven/>通过标签调用它

任何帮助都会非常感激。非常感谢你。

4

1 回答 1

1

要自定义图例框颜色,您需要使用自定义 HTML 图例,您可以使用 CSS 指定它,对于粉红色网格线,您可以使用可编写脚本的选项。对于两者,请参见示例:

const getOrCreateLegendList = (chart, id) => {
  const legendContainer = document.getElementById(id);
  let listContainer = legendContainer.querySelector('ul');

  if (!listContainer) {
    listContainer = document.createElement('ul');
    listContainer.style.display = 'flex';
    listContainer.style.flexDirection = 'row';
    listContainer.style.margin = 0;
    listContainer.style.padding = 0;

    legendContainer.appendChild(listContainer);
  }

  return listContainer;
};

const htmlLegendPlugin = {
  id: 'htmlLegend',
  afterUpdate(chart, args, options) {
    const ul = getOrCreateLegendList(chart, options.containerID);

    // Remove old legend items
    while (ul.firstChild) {
      ul.firstChild.remove();
    }

    // Reuse the built-in legendItems generator
    const items = chart.options.plugins.legend.labels.generateLabels(chart);

    items.forEach(item => {
      const li = document.createElement('li');
      li.style.alignItems = 'center';
      li.style.cursor = 'pointer';
      li.style.display = 'flex';
      li.style.flexDirection = 'row';
      li.style.marginLeft = '10px';

      li.onclick = () => {
        const {
          type
        } = chart.config;
        if (type === 'pie' || type === 'doughnut') {
          // Pie and doughnut charts only have a single dataset and visibility is per item
          chart.toggleDataVisibility(item.index);
        } else {
          chart.setDatasetVisibility(item.datasetIndex, !chart.isDatasetVisible(item.datasetIndex));
        }
        chart.update();
      };

      // Color box
      const boxSpan = document.createElement('span');
      boxSpan.style.background = options.legendBoxColor || item.fillStyle;
      boxSpan.style.borderColor = item.strokeStyle;
      boxSpan.style.borderWidth = item.lineWidth + 'px';
      boxSpan.style.display = 'inline-block';
      boxSpan.style.height = '20px';
      boxSpan.style.marginRight = '10px';
      boxSpan.style.width = '20px';

      // Text
      const textContainer = document.createElement('p');
      textContainer.style.color = options.legendTextColor || item.fontColor;
      textContainer.style.margin = 0;
      textContainer.style.padding = 0;
      textContainer.style.textDecoration = item.hidden ? 'line-through' : '';

      const text = document.createTextNode(item.text);
      textContainer.appendChild(text);

      li.appendChild(boxSpan);
      li.appendChild(textContainer);
      ul.appendChild(li);
    });
  }
};

const options = {
  type: 'bar',
  data: {
    labels: ["Red", "Blue", "Yellow", "Green", "Purple", "Orange"],
    datasets: [{
      label: '# of Votes',
      data: [12, 19, 3, 5, 2, 3],
      borderWidth: 1,
      backgroundColor: ["Red", "Blue", "Yellow", "Green", "Purple", "Orange"]
    }]
  },
  options: {
    scales: {
      x: {
        grid: {
          color: (line) => ((line.index === 2 || line.index === 3) ? 'pink' : 'rgba(0,0,0,0.1)'),
          lineWidth: (line) => ((line.index === 2 || line.index === 3) ? 6 : 1)
        }
      }
    },
    plugins: {
      htmlLegend: {
        // ID of the container to put the legend in
        containerID: 'legendContainer',
        legendBoxColor: 'blue',
        legendTextColor: 'blue'
      },
      legend: {
        display: false,
      }
    }
  },
  plugins: [htmlLegendPlugin]
}

const ctx = document.getElementById('chartJSContainer').getContext('2d');
new Chart(ctx, options);
<body>
  <div id="legendContainer"></div>
  <canvas id="chartJSContainer" width="600" height="400"></canvas>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/3.0.2/chart.js"></script>
</body>

于 2021-05-26T14:48:46.593 回答