如何在Vue-ApexCharts中对范围条形图的y轴标签进行排序?

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

How to sort yaxis label in range bar chart in vue-apexcharts?

问题

我想绘制一个水平条形图,每个条形的颜色在某个范围内不同,

我发现Vue-Apexcharts有一个rangeBar类型的图表可以做到这一点。

但我希望y轴标签(05/23~05/30)能按升序显示,应该如何做?

或者有人可以推荐我一个更好的绘图套件(用于Vue.js)来实现这个吗?

非常感谢。

如何在Vue-ApexCharts中对范围条形图的y轴标签进行排序?

英文:

I want to draw a horizontal bar chart and each bar has different color in some range,

I found Vue-Apexcharts has rangeBar type chart can do this.

But I want yAxis label (05/23~05/30) can show in ascending order, how can I do?

Or can anyone recommend me a better drawing suite (for Vue.js) to do this?

Thank you so much.

如何在Vue-ApexCharts中对范围条形图的y轴标签进行排序?

答案1

得分: -1

要按升序排序y轴标签,您可以使用sort函数对传递给图表组件的数据数组进行排序。以下是一个示例:

<template>
  <div>
    <apexchart type="rangeBar" :options="options" :series="series"></apexchart>
  </div>
</template>

<script>
import VueApexCharts from 'vue-apexcharts'

export default {
  components: {
    apexchart: VueApexCharts,
  },
  data() {
    return {
      options: {
        plotOptions: {
          bar: {
            horizontal: true,
          },
        },
        xaxis: {
          categories: ['Category 1', 'Category 2', 'Category 3'],
        },
        yaxis: {
          labels: {
            formatter: function(val) {
              return val.split("~").shift();
            },
          },
        },
      },
      series: [
        {
          data: [
            {
              x: 'Category 1',
              y: ['05/23~05/30', '#FF0000'],
            },
            {
              x: 'Category 2',
              y: ['05/24~05/31', '#00FF00'],
            },
            {
              x: 'Category 3',
              y: ['05/25~06/01', '#0000FF'],
            },
          ],
        },
      ],
    };
  },
  mounted() {
    // Sort data array by y label
    this.series[0].data.sort((a, b) => {
      const [startA] = a.y[0].split('~');
      const [startB] = b.y[0].split('~');
      return new Date(startA) - new Date(startB);
    });
  },
};
</script>

在这个示例中,我在mounted生命周期钩子中对series[0].data数组使用了sort函数,以便按范围的开始日期(y轴标签中的~字符之前的部分)进行排序。我使用了split函数来提取开始日期,然后使用Date构造函数将其转换为可排序的值。

至于其他Vue.js图表库的推荐,您可以查看以下选项:

英文:

To sort the y-axis labels in ascending order, you can use the sort function to sort the data array that you're passing to the chart component. Here's an example:

&lt;template&gt;
  &lt;div&gt;
    &lt;apexchart type=&quot;rangeBar&quot; :options=&quot;options&quot; :series=&quot;series&quot;&gt;&lt;/apexchart&gt;
  &lt;/div&gt;
&lt;/template&gt;

&lt;script&gt;
import VueApexCharts from &#39;vue-apexcharts&#39;

export default {
  components: {
    apexchart: VueApexCharts,
  },
  data() {
    return {
      options: {
        plotOptions: {
          bar: {
            horizontal: true,
          },
        },
        xaxis: {
          categories: [&#39;Category 1&#39;, &#39;Category 2&#39;, &#39;Category 3&#39;],
        },
        yaxis: {
          labels: {
            formatter: function(val) {
              return val.split(&quot;~&quot;).shift();
            },
          },
        },
      },
      series: [
        {
          data: [
            {
              x: &#39;Category 1&#39;,
              y: [&#39;05/23~05/30&#39;, &#39;#FF0000&#39;],
            },
            {
              x: &#39;Category 2&#39;,
              y: [&#39;05/24~05/31&#39;, &#39;#00FF00&#39;],
            },
            {
              x: &#39;Category 3&#39;,
              y: [&#39;05/25~06/01&#39;, &#39;#0000FF&#39;],
            },
          ],
        },
      ],
    };
  },
  mounted() {
    // Sort data array by y label
    this.series[0].data.sort((a, b) =&gt; {
      const [startA] = a.y[0].split(&#39;~&#39;);
      const [startB] = b.y[0].split(&#39;~&#39;);
      return new Date(startA) - new Date(startB);
    });
  },
};
&lt;/script&gt;

In this example, I'm using the sort function on the series[0].data array in the mounted lifecycle hook to sort the data by the start date of the range (the first part of the y-axis label, before the ~ character). I'm using the split function to extract the start date and then using the Date constructor to transform it into a sortable value.

As for recommendations for other charting libraries for Vue.js, you can check out:

huangapple
  • 本文由 发表于 2023年6月16日 15:42:16
  • 转载请务必保留本文链接:https://go.coder-hub.com/76487975.html
匿名

发表评论

匿名网友

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

确定