【问题标题】:Make a histogram in ApexCharts.js在 ApexCharts.js 中制作直方图
【发布时间】:2022-11-13 23:10:56
【问题描述】:

我正在尝试使用 ApexCharts(更具体地说,React-ApexCharts,但我认为这没有什么区别)创建直方图。

我之后的输出示例: 上面的示例最初是使用 Chart.js (source) 创建的。其中一个条形的蓝色阴影无关紧要。

即使“直方图”被列为 ApexCharts (reference) 中的图表类型之一,我也无法重现上面的示例。我无法解决的问题是:

  • 在 x 轴上有 number of bars + 1 的标签数量(即在上面的示例中,有 10 个标签和 9 个条形)
  • 在标签之间严格定位条形
  • 有一个工具提示引用每个条的相邻标签(例如上面的Hours: 6 - 7)

我卡住的示例代码:https://codesandbox.io/s/dreamy-wiles-8mbp3e

【问题讨论】:

    标签: javascript reactjs typescript histogram apexcharts


    【解决方案1】:

    类似于您的图像的东西,以及您列出的要求可以这样设置:

    const yData = [5,8,24,16,32,42,30,17,11];
        const options = {
            chart: {
                type: 'bar'
            },
            series: [{
                name: 'visitors',
                data: Array.from({length: yData.length},
                    (_, i)=>({
                        x: 0.5+i,
                        y: yData[i],
                        ... i === 4 ? ({fillColor: 'rgba(32, 120, 255, 0.4)', strokeColor: '#80afff'}) : ({})
                    }))
            }],
            plotOptions:{
                bar: {
                    columnWidth: "95%",
                    strokeWidth: 2,
                    borderRadius: 5,
                    borderRadiusApplication: "end",
                }
            },
            fill:{
                colors: '#ff4040',
                opacity: 0.3,
            },
            stroke:{
                width: 2,
                colors: ['#ee8080']
            },
            dataLabels: {enabled: false},
            grid: {
                xaxis: {
                    lines: {
                        show: true
                    }
                },
                yaxis: {
                    lines: {
                        show: true
                    }
                }
            },
            xaxis: {
                type: "numeric",
                min: 0,
                max: yData.length,
                tickAmount: yData.length,
                labels: {formatter: x => x /*Math.round(x)*/},
                title: {text: "Hours", offsetY: 70},
                axisBorder: {
                    color: "#000000"
                }
            },
            yaxis: {
                title: {text: "Visitors"},
                min: 0,
                max: Math.max(...yData),
                axisBorder: {
                    show: true,
                    color: "#000000"
                }
            },
            tooltip:{
                onDatasetHover: {
                    highlightDataSeries: true,
                },
                x: {formatter: x=>{return 'Hours '+(x-0.5)+'-'+(x+0.5);}}
            }
        }
    
        const chart = new ApexCharts(document.querySelector("#chart"), options);
    
        chart.render();
    <script src="https://cdn.jsdelivr.net/npm/apexcharts"></script>
    <div id="chart" style="border: 1px solid #444">
    </div>

    如果您列出了Chart.js 代码,您会更好地了解您的所有要求。

    【讨论】:

    • Chart.js 代码链接在 OP (copy of the link)
    • @swimmer 抱歉,我一定是错过了,或者使用了您帖子的早期版本。
    • 满足所有要求!我学到了什么: (1) 要在 ApexCharts.js 中构建直方图,输入 bar 比输入 histogram 效果更好; (2) 将x 和y 都传递给series 数据; (3) 将xaxis 中的options 解耦以在刻度线之间设置条形。谢谢!
    【解决方案2】:

    @kikon 在 React-ApexCharts 中接受的答案的改编:

    https://codesandbox.io/s/zealous-raman-02e7qk

    import "./styles.css";
    import Chart from "react-apexcharts";
    
    export default function App() {
      const yData = [5, 8, 24, 16, 32, 42, 30, 17, 11];
      const series = [
        {
          name: "visitors",
          data: Array.from({ length: yData.length }, (_, i) => ({
            x: 0.5 + i,
            y: yData[i],
            ...(i === 4
              ? { fillColor: "rgba(32, 120, 255, 0.4)", strokeColor: "#80afff" }
              : {})
          }))
        }
      ];
      const options = {
        chart: {
          type: "bar"
        },
        plotOptions: {
          bar: {
            columnWidth: "95%",
            strokeWidth: 2,
            borderRadius: 5,
            borderRadiusApplication: "end"
          }
        },
        fill: {
          colors: "#ff4040",
          opacity: 0.3
        },
        stroke: {
          width: 2,
          colors: ["#ee8080"]
        },
        dataLabels: { enabled: false },
        grid: {
          xaxis: {
            lines: {
              show: true
            }
          },
          yaxis: {
            lines: {
              show: true
            }
          }
        },
        xaxis: {
          type: "numeric",
          min: 0,
          max: yData.length,
          tickAmount: yData.length,
          labels: { formatter: (x) => x /*Math.round(x)*/ },
          title: { text: "Hours", offsetY: 70 },
          axisBorder: {
            color: "#000000"
          }
        },
        yaxis: {
          title: { text: "Visitors" },
          min: 0,
          max: Math.max(...yData),
          axisBorder: {
            show: true,
            color: "#000000"
          }
        },
        tooltip: {
          onDatasetHover: {
            highlightDataSeries: true
          },
          x: {
            formatter: (x) => {
              return "Hours " + (x - 0.5) + "-" + (x + 0.5);
            }
          }
        }
      };
      return <Chart options={options} series={series} type="bar" height={260} />;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-08-14
      • 1970-01-01
      • 2016-07-22
      • 2019-04-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多