Skip to content
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

Exercise/highcharts 2 #3

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions highcharts-api/highcharts/2-polar-rings/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
<meta name="viewport" content="width=device-width, initial-scale=1">

<script src="https://code.highcharts.com/highcharts.js"></script>
<script src="https://code.highcharts.com/highcharts-more.js"></script>

</head>

Expand Down
99 changes: 99 additions & 0 deletions highcharts-api/highcharts/2-polar-rings/main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
const generateRandomSeriesData = () =>
Array.from({ length: 3 }, () => Math.floor(Math.random() * 10));

const chart = Highcharts.chart('container', {
chart: {
type: 'column',
polar: true,
events: {
load() {
const yAxis = this.yAxis[0],
dataMax = yAxis.dataMax;

yAxis.update({
max: dataMax * 2,
});

yAxis.addPlotLine({
dashStyle: 'Dash',
value: dataMax * 1.5,
width: 2
});
},
render() {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice! I have only two suggestions:

  • radius is not quite responsive this way (it changes via Math.random() every time when you redraw the chart, but it should only base on the value from the chart and scale relative to the chart ( toPixels() is a good choice 🥇 )
  • if you first check that the circle doesn't exist, you avoid a few code lines

Generally, you can simplify it following way:

    events: {
      render: function() {
        const chart = this,
          yAxis = this.yAxis[0];

        if (!chart.customCircle) {
          chart.customCircle = chart.renderer
            .circle()
            .attr({
              stroke: "blue",
              "stroke-width": 3,
              fill: "none"
            })
            .add();
        }

        chart.customCircle.attr({
          x: chart.yAxis[0].center[0] + chart.plotTop,
          y: chart.yAxis[0].center[1] + chart.plotLeft,
          r: chart.yAxis[0].toPixels(chart.yAxis[0].dataMax)
        });

      }
    }

const chart = this,
yAxis = chart.yAxis[0],
redCircle = chart.redCircle;

if (redCircle) {
redCircle.attr({
cx: chart.plotWidth / 2 + chart.plotLeft,
cy: chart.plotHeight / 2 + chart.plotTop,
r: yAxis.toPixels(
Math.random() * yAxis.dataMax * 2,
true
)
});
} else {
const renderedRedCircle = chart.renderer
.circle(
chart.plotWidth / 2 + chart.plotLeft,
chart.plotHeight / 2 + chart.plotTop,
100
)
.attr({
fill: 'white',
stroke: 'red',
'stroke-width': 2
})
.add();

chart.redCircle = renderedRedCircle;
}
},
},
},
title: {
text: undefined,
},
xAxis: {
categories: ['Jan', 'Feb', 'Mar']
},
yAxis: {
plotLines: [
{
width: 2,
color: 'green',
value: 15
}
]
},
series: [
{
name: 'Tokyo',
data: generateRandomSeriesData()
},
{
name: 'New York',
data: generateRandomSeriesData()
},
{
name: 'London',
data: generateRandomSeriesData()
},
],
plotOptions: {
series: {
dataLabels: {
enabled: true,
formatter() {
return this.y === this.series.yAxis.dataMax ? 'max' : '';
}
}
},
column: {
pointPadding: 0,
groupPadding: 0,
}
}
});