-
Notifications
You must be signed in to change notification settings - Fork 0
Highcharts: Stacked bar #10
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,142 @@ | ||
| const buttonAttrs = { | ||
| height: 6, | ||
| zIndex: 10, | ||
| stroke: 'blue', | ||
| }; | ||
| const buttonCSS = { 'font-size': '13px' }; | ||
| const labelCSS = { 'font-size': '10px' }; | ||
|
|
||
| const chartEvents = { | ||
| render: function() { | ||
| const chart = this; | ||
|
|
||
| if (chart.svgElements) { | ||
| chart.svgElements.forEach(e => e.destroy()); | ||
|
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's better to update coords instead. Should fix this
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. indeed it's better :) |
||
| } | ||
| chart.svgElements = []; | ||
|
|
||
| const buttons = addButtons(chart); | ||
| chart.svgElements.push(...buttons); | ||
|
|
||
| const labels = addLabels(chart); | ||
| chart.svgElements.push(...labels); | ||
| } | ||
| } | ||
|
|
||
| Highcharts.chart('container', { | ||
| dataTable: { | ||
| columns: { | ||
| Categories: ['Data', 'Emails', 'Duplicates', 'Support'], | ||
| Orange: [100, 130, 35, 30], | ||
| Green: [15, 0, 15, 10], | ||
| Blue: [110, 110, 30], | ||
| Red: [15, 0, 10, 5] | ||
| } | ||
| }, | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why are you using dataTable here? feels like an overkill
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't know. It was just interesting how it works |
||
| chart: { | ||
| type: 'bar', | ||
| events: chartEvents, | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I see that you like to keep the config small and define all helpers outside, which is a common pattern, but not here - with such small demos we tend to keep all the logic just in the chart config, here under relevant chart events - can you refactor your solution to follow that pattern, please? |
||
| marginTop: 20 | ||
| }, | ||
| title: { | ||
| text: null | ||
| }, | ||
| legend: { | ||
| enabled: false | ||
| }, | ||
| xAxis: { | ||
| type: 'category', | ||
| lineWidth: 0, | ||
| gridLineWidth: 1 | ||
| }, | ||
| yAxis: { | ||
| title: { text: 'Amount' }, | ||
| softMax: 400, | ||
| gridLineWidth: 0, | ||
| stackLabels: { | ||
| enabled: true, | ||
| formatter: function() { | ||
| return this.total + ' K'; | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nice, but actually why do we need formatter here? There is a closely related, simpler and more performant way ;) |
||
| } | ||
| } | ||
| }, | ||
| plotOptions: { | ||
| series: { | ||
| dataMapping: { | ||
| name: 'Categories' | ||
| }, | ||
| stacking: 'normal' | ||
| } | ||
| }, | ||
| series: [{ | ||
| dataMapping: { | ||
| y: 'Orange' | ||
| } | ||
| }, { | ||
| dataMapping: { | ||
| y: 'Green' | ||
| } | ||
| }, { | ||
| dataMapping: { | ||
| y: 'Blue' | ||
| } | ||
| }, { | ||
| dataMapping: { | ||
| y: 'Red' | ||
| } | ||
| }] | ||
| }); | ||
|
|
||
| function addLabels(chart) { | ||
| const labelY = -5; | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. you are hardcoding coordinates, and it work, but have you seen this useful api option, which we tend you use for such cases? https://api.highcharts.com/class-reference/Highcharts.Axis#toPixels |
||
| const label1X = 3, | ||
| label2X = chart.plotLeft - 3, | ||
| label3X = chart.plotWidth; | ||
|
|
||
| const label1 = chart.renderer | ||
| .label('Issue', label1X, labelY) | ||
| .css(labelCSS) | ||
| .add(); | ||
|
|
||
| const label2 = chart.renderer | ||
| .label('Record Count', label2X, labelY) | ||
| .css(labelCSS) | ||
| .add(); | ||
|
|
||
| const label3 = chart.renderer | ||
| .label('Action', label3X, labelY) | ||
| .css(labelCSS) | ||
| .add(); | ||
|
|
||
| return [label1, label2, label3]; | ||
| } | ||
|
|
||
| function addButtons(chart) { | ||
| const buttonX = chart.plotWidth; | ||
| const buttons = []; | ||
|
|
||
| chart.series[0].points.forEach((point, i) => { | ||
| const pointX = chart.plotTop + point.shapeArgs.x; | ||
| const pointWidth = point.shapeArgs.width; | ||
|
|
||
| const button = chart.renderer | ||
| .button( | ||
| 'How to fix', | ||
| buttonX, | ||
| pointX, | ||
| function () {}, | ||
| buttonAttrs | ||
| ) | ||
| .css(buttonCSS) | ||
| .add(); | ||
|
|
||
| buttons.push(button); | ||
|
|
||
| // You can only get correct button height after it was rendered. | ||
| button.attr({ | ||
| y: pointX + pointWidth / 2 - button.height / 2 | ||
| }); | ||
| }); | ||
|
|
||
| return buttons; | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's better to set such attribs where you create elements with renderer, no need for such an abstraction here