You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Flutter BarChart:替换横轴数字为月份名并添加轴标题

Solution

1. Replace Numeric Months with Names on X-axis

Swap out the numeric month values for monthName directly in your chart's X-axis data source. Ensure your dataset references the month names instead of numbers to display "Jan", "Feb", etc.

2. Add Axis Titles

Configure your chart's axis settings to enable and set the required titles for both axes.


Example Code (Chart.js)

Here’s how to adjust your code to meet both requirements:

// Sample reportData structure
// const reportData = [
//   { month: 1, monthName: "Jan", value: 8000 },
//   { month: 2, monthName: "Feb", value: 12000 },
//   { month: 3, monthName: "Mar", value: 9500 }
// ];

const chartCtx = document.getElementById('monthlyBarChart').getContext('2d');
const monthlyChart = new Chart(chartCtx, {
  type: 'bar',
  data: {
    // Use monthName for X-axis labels
    labels: reportData.map(item => item.monthName),
    datasets: [{
      label: 'Monthly Amount',
      data: reportData.map(item => item.value),
      backgroundColor: '#2196F3',
      borderColor: '#1976D2',
      borderWidth: 1
    }]
  },
  options: {
    scales: {
      x: {
        // Add X-axis title
        title: {
          display: true,
          text: 'Months',
          font: {
            size: 14
          }
        }
      },
      y: {
        // Add Y-axis title
        title: {
          display: true,
          text: 'Rupees',
          font: {
            size: 14
          }
        },
        beginAtZero: true
      }
    }
  }
});

Key Adjustments:

  • X-axis Labels: The labels array now pulls directly from reportData.monthName instead of numeric months.
  • Axis Titles: Enabled via scales.x.title and scales.y.title, with display: true and custom text set for each axis.

If you’re using a different library (like ApexCharts, D3.js), the core logic stays the same:

  1. Map your X-axis to monthName values.
  2. Locate the axis title configuration in the library’s docs (e.g., xaxis.title.text in ApexCharts) and set the required text.

内容的提问来源于stack exchange,提问作者Vidhyanand

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.12 23:15:01