-
Notifications
You must be signed in to change notification settings - Fork 0
H17: Create chart with csv data #13
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
Open
dqmrf
wants to merge
3
commits into
main
Choose a base branch
from
highcharts/17-data-module
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1 +1,143 @@ | ||
| const data = document.getElementById('csv').innerHTML; | ||
| const data = document.getElementById('csv').innerHTML; | ||
| const T_UNITS = [ 'C', 'F' ]; | ||
| let currentTUnit = 1; | ||
|
|
||
| const toMinutes = value => { | ||
| const [h, m] = value.split(':').map(Number); | ||
| return h * 60 + m; | ||
| }; | ||
|
|
||
| const toTimeStr = minutes => { | ||
| const h = Math.floor(minutes / 60), | ||
| m = minutes % 60; | ||
| const toStr = v => String(v).padStart(2, '0'); | ||
| return `${toStr(h)}:${toStr(m)}`; | ||
| }; | ||
|
|
||
| const chart = Highcharts.chart('container', { | ||
| chart: { | ||
| events: { | ||
| load() { | ||
| const chart = this; | ||
|
|
||
| chart.tUnitSwitchBtn = chart.renderer | ||
| .button(T_UNITS[currentTUnit], 0, 0, function() { | ||
| const temperatureSeries = []; | ||
| chart.series.forEach(s => { | ||
| if (s.name === 'tempC') temperatureSeries[0] = s; | ||
| if (s.name === 'tempF') temperatureSeries[1] = s; | ||
| }); | ||
|
|
||
| temperatureSeries[currentTUnit].hide(); | ||
| currentTUnit ^= 1; | ||
| temperatureSeries[currentTUnit].show(); | ||
|
|
||
| this.attr({ text: T_UNITS[currentTUnit] }); | ||
|
|
||
| chart.yAxis[0].update({ | ||
| title: { | ||
| text: T_UNITS[currentTUnit] | ||
| }, | ||
| labels: { | ||
| format: `{value}°${T_UNITS[currentTUnit]}` | ||
| } | ||
| }); | ||
| }) | ||
| .add(); | ||
| } | ||
| } | ||
| }, | ||
| title: { | ||
| text: 'Temperature' | ||
| }, | ||
| yAxis: [{ | ||
| title: { text: T_UNITS[currentTUnit] }, | ||
| height: '45%', | ||
| lineWidth: 2, | ||
| labels: { | ||
| format: `{value}°${T_UNITS[currentTUnit]}` | ||
| } | ||
| }, { | ||
| title: { text: 'Rainfall' }, | ||
| height: '45%', | ||
| top: '55%', | ||
| offset: 0, | ||
| lineWidth: 2, | ||
| labels: { | ||
| formatter() { | ||
| return toTimeStr(this.value); | ||
| } | ||
| } | ||
| }], | ||
| legend: { | ||
| enabled: false | ||
| }, | ||
| data: { | ||
| csv: data, | ||
| firstRowAsNames: true, | ||
| itemDelimiter: ',', | ||
| parsed(columns) { | ||
| columns.forEach(col => { | ||
| const colName = col[0]; | ||
| if (colName === 'rainStart' || colName === 'rainEnd') { | ||
| for (let i = 1; i < col.length; i++) { | ||
| col[i] = toMinutes(col[i]); | ||
| } | ||
| } | ||
| }); | ||
| }, | ||
| complete(options) { | ||
| let tempF = { | ||
| visible: T_UNITS[currentTUnit] === 'F' | ||
| }; | ||
| let tempC = { | ||
| visible: T_UNITS[currentTUnit] === 'C' | ||
| }; | ||
| let rainfall = { | ||
| yAxis: 1, | ||
| type: 'columnrange', | ||
| name: 'rainfall', | ||
| pointStart: 0, | ||
| data: [], | ||
| tooltip: { | ||
| pointFormatter() { | ||
| return ` | ||
| <span style="color:${this.color}">●</span> | ||
| ${this.series.name}: <b>${toTimeStr(this.low)} – ${toTimeStr(this.high)}</b><br/> | ||
| `; | ||
| } | ||
| } | ||
| }; | ||
|
|
||
| options.series.forEach(s => { | ||
| switch (s.name) { | ||
| case 'tempC': | ||
| tempC = { ...tempC, ...s }; | ||
| break; | ||
|
|
||
| case 'tempF': | ||
| tempF = { ...tempF, ...s }; | ||
| break; | ||
|
|
||
| case 'rainStart': | ||
| rainfall.pointStart = s.pointStart; | ||
| s.data.forEach((data, i) => { | ||
| rainfall.data[i] ||= []; | ||
| rainfall.data[i][0] = data[0]; | ||
| rainfall.data[i][1] = data[1]; | ||
| }); | ||
| break; | ||
|
|
||
| case 'rainEnd': | ||
| s.data.forEach((data, i) => { | ||
| rainfall.data[i] ||= []; | ||
| rainfall.data[i][2] = data[1]; | ||
| }); | ||
| break; | ||
| } | ||
| }); | ||
|
|
||
| options.series = [ tempC, tempF, rainfall ]; | ||
| } | ||
| } | ||
| }); | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
formatinstead offormatter(better e.g. for JSON configs)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.
Format will not work here because when you press the button:
currentTUnitvariable changes and axis text must change as well.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.
Then perhaps you should update the yAxis.labels.format on a button click as well? Only a few lines of additional code, but it'll work for more specific cases.
You don't have to change it now, but please keep it in mind -
formatis usually better thanformatter. We had many users on support with problems when the formatter wasn't working in specific cases, e.g. when the chart config had to be a JSON string (no functions, only strings).Soon you'll work with Highcharts core code, and build API and documentation, and you'll have to use
formatonly.ps. I can see the
.attr()on the yAxis text - usingyAxis.update({ title: { text: ... } })is also better because you store all real values on the chart (with.attr, you only change DOM)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.
Okay. I'll keep that in mind.