How to Create a Chart with Shared X-Axis Labels in HTMLCSS? - Stack Overflow

How to Create a Chart with Shared X-Axis Labels in HTMLCSS?I'm trying to create a line chart whe

How to Create a Chart with Shared X-Axis Labels in HTML/CSS?

I'm trying to create a line chart where the x-axis labels match the column headers of a data table displayed below the chart, similar to how it's done in MS Excel with a DataTable as a chart element.

Example

Here's what I'm looking for:

  • X-Axis Labels: Jan, Feb, Mar, Apr, May, Jun
  • Table Headers: Should match the x-axis labels
  • Data Lines: Two lines (Sales and Volume)

I want the result to display a chart that integrates seamlessly with the data table below it.

What I've Tried

I've explored using Chart.js but haven't found any examples or documentation that address this specific requirement. I'm open to alternative tools or methods in HTML/CSS (or JavaScript) to achieve this.

Specific Challenges:

  • Aligning chart x-axis with table headers
  • Maintaining consistent label formatting
  • Creating a responsive design that works across different devices

Question

What's the most effective way to create a line chart with x-axis labels that directly correspond to a data table's column headers?

How to Create a Chart with Shared X-Axis Labels in HTML/CSS?

I'm trying to create a line chart where the x-axis labels match the column headers of a data table displayed below the chart, similar to how it's done in MS Excel with a DataTable as a chart element.

Example

Here's what I'm looking for:

  • X-Axis Labels: Jan, Feb, Mar, Apr, May, Jun
  • Table Headers: Should match the x-axis labels
  • Data Lines: Two lines (Sales and Volume)

I want the result to display a chart that integrates seamlessly with the data table below it.

What I've Tried

I've explored using Chart.js but haven't found any examples or documentation that address this specific requirement. I'm open to alternative tools or methods in HTML/CSS (or JavaScript) to achieve this.

Specific Challenges:

  • Aligning chart x-axis with table headers
  • Maintaining consistent label formatting
  • Creating a responsive design that works across different devices

Question

What's the most effective way to create a line chart with x-axis labels that directly correspond to a data table's column headers?

Share Improve this question asked Mar 24 at 17:18 Carl GRIMALDICarl GRIMALDI 112 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

I can suggest you to use Apache ECharts as a powerful lib that can cover your needs. Just to set formatter for xAxis.axisLabel like this:

const getAxisXFormattedLabel = (yValue: string, index: number) => {
    const xData = data.find(x => x.yValue === yValue);
    
    const month = xData.month;
    const sales = xData.sales;
    const volume = xData.volume;

    return `${month}\n${sales}\n${volume}`;
}

Hope it works well for you!

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744237531a4564542.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信