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

Highcharts多列图配置咨询:添加城市年度平均线

解决方案:为每个城市添加年度平均线的Highcharts柱状图

Hey there! Let's tackle this problem step by step since you're new to HTML/JS and Highcharts. I've worked on similar setups before, so here's how you can add those annual average lines for each city correctly:

核心思路

Since you already have the annual averages from your SQL query, the key is to pair each city's monthly column data with a dedicated horizontal line series that uses the city's average value for all 12 months. This ensures each city gets its own distinct average line, rather than a single global line.

完整代码示例

First, let's assume your data structure (adjust this to match your actual SQL output):

// Sample data (replace with your actual SQL-derived data)
const cityDeliveryData = [
  {
    cityName: 'New York',
    monthlyVolumes: [420, 380, 450, 470, 510, 550, 580, 560, 520, 480, 440, 410],
    annualAverage: 480 // From your SQL query
  },
  {
    cityName: 'London',
    monthlyVolumes: [350, 330, 370, 390, 420, 450, 470, 460, 430, 400, 370, 340],
    annualAverage: 400
  },
  {
    cityName: 'Tokyo',
    monthlyVolumes: [500, 480, 530, 560, 600, 630, 650, 640, 610, 570, 530, 500],
    annualAverage: 560
  }
];

Now, build your Highcharts configuration with both column and line series:

// Build the series array: one column series + one line series per city
const chartSeries = [];

cityDeliveryData.forEach((city, index) => {
  // Add monthly column data for the city
  chartSeries.push({
    name: city.cityName,
    type: 'column',
    data: city.monthlyVolumes,
    color: Highcharts.getOptions().colors[index] // Use Highcharts' default color palette
  });

  // Add annual average line for the city
  chartSeries.push({
    name: `${city.cityName} Annual Avg`,
    type: 'line',
    data: Array(12).fill(city.annualAverage), // Repeat average for all 12 months
    marker: { enabled: false }, // Hide line markers for a clean look
    dashStyle: 'dash', // Use dashed line to distinguish from columns
    lineWidth: 2,
    color: Highcharts.getOptions().colors[index], // Match column color for clarity
    zIndex: 5 // Ensure line appears above columns
  });
});

// Initialize the chart
Highcharts.chart('content-daily-delivery-status', {
  chart: {
    type: 'column',
    height: 500
  },
  title: {
    text: 'Monthly Delivery Volumes with Annual Averages'
  },
  xAxis: {
    categories: ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'],
    crosshair: true
  },
  yAxis: {
    min: 0,
    title: {
      text: 'Delivery Volume'
    }
  },
  tooltip: {
    shared: true, // Show all data points at the same x-axis position
    headerFormat: '<span style="font-size:10px">{point.key}</span><table>',
    pointFormat: '<tr><td style="color:{series.color};padding:0">{series.name}: </td>' +
      '<td style="padding:0"><b>{point.y:.0f}</b></td></tr>',
    footerFormat: '</table>',
    useHTML: true
  },
  plotOptions: {
    column: {
      pointPadding: 0.2,
      borderWidth: 0
    }
  },
  series: chartSeries
});

Key Fixes & Tips

  • Pairing Series: Each city gets two series (column + line) to keep the average tied directly to its data.
  • Line Styling: Using dashStyle: 'dash' and hiding markers makes the average lines easy to distinguish from columns without cluttering the chart.
  • Color Consistency: Reusing the same color for a city's column and line helps users connect the data instantly.
  • Tooltip Clarity: The shared: true tooltip lets users compare monthly volumes and the annual average at a glance.
  • Z-Index: Setting zIndex: 5 ensures the average lines sit on top of the columns, so they're never hidden.

Troubleshooting Common Issues

If your chart still isn't working as expected, check these:

  • Ensure your SQL-derived averages are correctly mapped to each city (no mismatched values).
  • Verify that the data array for the line series has exactly 12 values (one for each month).
  • Make sure the container element (content-daily-delivery-status) exists in your HTML with a defined height/width.

内容的提问来源于stack exchange,提问作者I.Ciano

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.26 10:05:02