highcharts-meteogram.js 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741
  1. /**
  2. * This is a complex demo of how to set up a Highcharts chart, coupled to a
  3. * dynamic source and extended by drawing image sprites, wind arrow paths
  4. * and a second grid on top of the chart. The purpose of the demo is to inpire
  5. * developers to go beyond the basic chart types and show how the library can
  6. * be extended programmatically. This is what the demo does:
  7. *
  8. * - Loads weather forecast from www.yr.no in form of an XML service. The XML
  9. * is translated on the Higcharts website into JSONP for the sake of the demo
  10. * being shown on both our website and JSFiddle.
  11. * - When the data arrives async, a Meteogram instance is created. We have
  12. * created the Meteogram prototype to provide an organized structure of the different
  13. * methods and subroutines associated with the demo.
  14. * - The parseYrData method parses the data from www.yr.no into several parallel arrays. These
  15. * arrays are used directly as the data option for temperature, precipitation
  16. * and air pressure. As the temperature data gives only full degrees, we apply
  17. * some smoothing on the graph, but keep the original data in the tooltip.
  18. * - After this, the options structure is build, and the chart generated with the
  19. * parsed data.
  20. * - In the callback (on chart load), we weather icons on top of the temperature series.
  21. * The icons are sprites from a single PNG image, placed inside a clipped 30x30
  22. * SVG <g> element. VML interprets this as HTML images inside a clipped div.
  23. * - Lastly, the wind arrows are built and added below the plot area, and a grid is
  24. * drawn around them. The wind arrows are basically drawn north-south, then rotated
  25. * as per the wind direction.
  26. */
  27. function Meteogram(xml, container) {
  28. // Parallel arrays for the chart data, these are populated as the XML/JSON file
  29. // is loaded
  30. this.symbols = [];
  31. this.symbolNames = [];
  32. this.precipitations = [];
  33. this.windDirections = [];
  34. this.windDirectionNames = [];
  35. this.windSpeeds = [];
  36. this.windSpeedNames = [];
  37. this.temperatures = [];
  38. this.pressures = [];
  39. // Initialize
  40. this.xml = xml;
  41. this.container = container;
  42. // Run
  43. this.parseYrData();
  44. }
  45. Meteogram.prototype.getHeight = function () {
  46. return 310;
  47. };
  48. Meteogram.prototype.getWidth = function () {
  49. return 800;
  50. };
  51. /**
  52. * Return weather symbol sprites as laid out at http://om.yr.no/forklaring/symbol/
  53. */
  54. Meteogram.prototype.getSymbolSprites = function (symbolSize) {
  55. return {
  56. '01d': {
  57. x: 0,
  58. y: 0
  59. },
  60. '01n': {
  61. x: symbolSize,
  62. y: 0
  63. },
  64. '16': {
  65. x: 2 * symbolSize,
  66. y: 0
  67. },
  68. '02d': {
  69. x: 0,
  70. y: symbolSize
  71. },
  72. '02n': {
  73. x: symbolSize,
  74. y: symbolSize
  75. },
  76. '03d': {
  77. x: 0,
  78. y: 2 * symbolSize
  79. },
  80. '03n': {
  81. x: symbolSize,
  82. y: 2 * symbolSize
  83. },
  84. '17': {
  85. x: 2 * symbolSize,
  86. y: 2 * symbolSize
  87. },
  88. '04': {
  89. x: 0,
  90. y: 3 * symbolSize
  91. },
  92. '05d': {
  93. x: 0,
  94. y: 4 * symbolSize
  95. },
  96. '05n': {
  97. x: symbolSize,
  98. y: 4 * symbolSize
  99. },
  100. '18': {
  101. x: 2 * symbolSize,
  102. y: 4 * symbolSize
  103. },
  104. '06d': {
  105. x: 0,
  106. y: 5 * symbolSize
  107. },
  108. '06n': {
  109. x: symbolSize,
  110. y: 5 * symbolSize
  111. },
  112. '07d': {
  113. x: 0,
  114. y: 6 * symbolSize
  115. },
  116. '07n': {
  117. x: symbolSize,
  118. y: 6 * symbolSize
  119. },
  120. '08d': {
  121. x: 0,
  122. y: 7 * symbolSize
  123. },
  124. '08n': {
  125. x: symbolSize,
  126. y: 7 * symbolSize
  127. },
  128. '19': {
  129. x: 2 * symbolSize,
  130. y: 7 * symbolSize
  131. },
  132. '09': {
  133. x: 0,
  134. y: 8 * symbolSize
  135. },
  136. '10': {
  137. x: 0,
  138. y: 9 * symbolSize
  139. },
  140. '11': {
  141. x: 0,
  142. y: 10 * symbolSize
  143. },
  144. '12': {
  145. x: 0,
  146. y: 11 * symbolSize
  147. },
  148. '13': {
  149. x: 0,
  150. y: 12 * symbolSize
  151. },
  152. '14': {
  153. x: 0,
  154. y: 13 * symbolSize
  155. },
  156. '15': {
  157. x: 0,
  158. y: 14 * symbolSize
  159. },
  160. '20d': {
  161. x: 0,
  162. y: 15 * symbolSize
  163. },
  164. '20n': {
  165. x: symbolSize,
  166. y: 15 * symbolSize
  167. },
  168. '20m': {
  169. x: 2 * symbolSize,
  170. y: 15 * symbolSize
  171. },
  172. '21d': {
  173. x: 0,
  174. y: 16 * symbolSize
  175. },
  176. '21n': {
  177. x: symbolSize,
  178. y: 16 * symbolSize
  179. },
  180. '21m': {
  181. x: 2 * symbolSize,
  182. y: 16 * symbolSize
  183. },
  184. '22': {
  185. x: 0,
  186. y: 17 * symbolSize
  187. },
  188. '23': {
  189. x: 0,
  190. y: 18 * symbolSize
  191. }
  192. };
  193. };
  194. /**
  195. * Function to smooth the temperature line. The original data provides only whole degrees,
  196. * which makes the line graph look jagged. So we apply a running mean on it, but preserve
  197. * the unaltered value in the tooltip.
  198. */
  199. Meteogram.prototype.smoothLine = function (data) {
  200. var i = data.length,
  201. sum,
  202. value;
  203. while (i--) {
  204. data[i].value = value = data[i].y; // preserve value for tooltip
  205. // Set the smoothed value to the average of the closest points, but don't allow
  206. // it to differ more than 0.5 degrees from the given value
  207. sum = (data[i - 1] || data[i]).y + value + (data[i + 1] || data[i]).y;
  208. data[i].y = Math.max(value - 0.5, Math.min(sum / 3, value + 0.5));
  209. }
  210. };
  211. /**
  212. * Callback function that is called from Highcharts on hovering each point and returns
  213. * HTML for the tooltip.
  214. */
  215. Meteogram.prototype.tooltipFormatter = function (tooltip) {
  216. // Create the header with reference to the time interval
  217. var index = tooltip.points[0].point.index,
  218. ret = '<small>' + Highcharts.dateFormat('%A, %b %e, %H:%M', tooltip.x) + '-' +
  219. Highcharts.dateFormat('%H:%M', tooltip.points[0].point.to) + '</small><br>';
  220. // Symbol text
  221. ret += '<b>' + this.symbolNames[index] + '</b>';
  222. ret += '<table>';
  223. // Add all series
  224. Highcharts.each(tooltip.points, function (point) {
  225. var series = point.series;
  226. ret += '<tr><td><span style="color:' + series.color + '">\u25CF</span> ' + series.name +
  227. ': </td><td style="white-space:nowrap">' + Highcharts.pick(point.point.value, point.y) +
  228. series.options.tooltip.valueSuffix + '</td></tr>';
  229. });
  230. // Add wind
  231. ret += '<tr><td style="vertical-align: top">\u25CF Wind</td><td style="white-space:nowrap">' + this.windDirectionNames[index] +
  232. '<br>' + this.windSpeedNames[index] + ' (' +
  233. Highcharts.numberFormat(this.windSpeeds[index], 1) + ' m/s)</td></tr>';
  234. // Close
  235. ret += '</table>';
  236. return ret;
  237. };
  238. /**
  239. * Draw the weather symbols on top of the temperature series. The symbols are sprites of a single
  240. * file, defined in the getSymbolSprites function above.
  241. */
  242. Meteogram.prototype.drawWeatherSymbols = function (chart) {
  243. var meteogram = this,
  244. symbolSprites = this.getSymbolSprites(30);
  245. $.each(chart.series[0].data, function (i, point) {
  246. var sprite,
  247. group;
  248. if (meteogram.resolution > 36e5 || i % 2 === 0) {
  249. sprite = symbolSprites[meteogram.symbols[i]];
  250. if (sprite) {
  251. // Create a group element that is positioned and clipped at 30 pixels width and height
  252. group = chart.renderer.g()
  253. .attr({
  254. translateX: point.plotX + chart.plotLeft - 15,
  255. translateY: point.plotY + chart.plotTop - 30,
  256. zIndex: 5
  257. })
  258. .clip(chart.renderer.clipRect(0, 0, 30, 30))
  259. .add();
  260. // Position the image inside it at the sprite position
  261. chart.renderer.image(
  262. '/fhem/tablet/lib/highcharts/graphics/meteogram-symbols-30px.png',
  263. -sprite.x,
  264. -sprite.y,
  265. 90,
  266. 570
  267. )
  268. .add(group);
  269. }
  270. }
  271. });
  272. };
  273. /**
  274. * Create wind speed symbols for the Beaufort wind scale. The symbols are rotated
  275. * around the zero centerpoint.
  276. */
  277. Meteogram.prototype.windArrow = function (name) {
  278. var level,
  279. path;
  280. // The stem and the arrow head
  281. path = [
  282. 'M', 0, 7, // base of arrow
  283. 'L', -1.5, 7,
  284. 0, 10,
  285. 1.5, 7,
  286. 0, 7,
  287. 0, -10 // top
  288. ];
  289. level = $.inArray(name, ['Calm', 'Light air', 'Light breeze', 'Gentle breeze', 'Moderate breeze',
  290. 'Fresh breeze', 'Strong breeze', 'Near gale', 'Gale', 'Strong gale', 'Storm',
  291. 'Violent storm', 'Hurricane']);
  292. if (level === 0) {
  293. path = [];
  294. }
  295. if (level === 2) {
  296. path.push('M', 0, -8, 'L', 4, -8); // short line
  297. } else if (level >= 3) {
  298. path.push(0, -10, 7, -10); // long line
  299. }
  300. if (level === 4) {
  301. path.push('M', 0, -7, 'L', 4, -7);
  302. } else if (level >= 5) {
  303. path.push('M', 0, -7, 'L', 7, -7);
  304. }
  305. if (level === 5) {
  306. path.push('M', 0, -4, 'L', 4, -4);
  307. } else if (level >= 6) {
  308. path.push('M', 0, -4, 'L', 7, -4);
  309. }
  310. if (level === 7) {
  311. path.push('M', 0, -1, 'L', 4, -1);
  312. } else if (level >= 8) {
  313. path.push('M', 0, -1, 'L', 7, -1);
  314. }
  315. return path;
  316. };
  317. /**
  318. * Draw the wind arrows. Each arrow path is generated by the windArrow function above.
  319. */
  320. Meteogram.prototype.drawWindArrows = function (chart) {
  321. var meteogram = this;
  322. $.each(chart.series[0].data, function (i, point) {
  323. var sprite, arrow, x, y;
  324. if (meteogram.resolution > 36e5 || i % 2 === 0) {
  325. // Draw the wind arrows
  326. x = point.plotX + chart.plotLeft + 7;
  327. // y = 255;
  328. y = meteogram.getHeight() - 55;
  329. if (meteogram.windSpeedNames[i] === 'Calm') {
  330. arrow = chart.renderer.circle(x, y, 10).attr({
  331. fill: 'none'
  332. });
  333. } else {
  334. arrow = chart.renderer.path(
  335. meteogram.windArrow(meteogram.windSpeedNames[i])
  336. ).attr({
  337. rotation: parseInt(meteogram.windDirections[i], 10),
  338. translateX: x, // rotation center
  339. translateY: y // rotation center
  340. });
  341. }
  342. arrow.attr({
  343. stroke: (Highcharts.theme && Highcharts.theme.contrastTextColor) || 'black',
  344. 'stroke-width': 1.5,
  345. zIndex: 5
  346. })
  347. .add();
  348. }
  349. });
  350. };
  351. /**
  352. * Draw blocks around wind arrows, below the plot area
  353. */
  354. Meteogram.prototype.drawBlocksForWindArrows = function (chart) {
  355. var xAxis = chart.xAxis[0],
  356. x,
  357. pos,
  358. max,
  359. isLong,
  360. isLast,
  361. i;
  362. for (pos = xAxis.min, max = xAxis.max, i = 0; pos <= max + 36e5; pos += 36e5, i += 1) {
  363. // Get the X position
  364. isLast = pos === max + 36e5;
  365. x = Math.round(xAxis.toPixels(pos)) + (isLast ? 0.5 : -0.5);
  366. // Draw the vertical dividers and ticks
  367. if (this.resolution > 36e5) {
  368. isLong = pos % this.resolution === 0;
  369. } else {
  370. isLong = i % 2 === 0;
  371. }
  372. chart.renderer.path(['M', x, chart.plotTop + chart.plotHeight + (isLong ? 0 : 28),
  373. 'L', x, chart.plotTop + chart.plotHeight + 32, 'Z'])
  374. .attr({
  375. 'stroke': chart.options.chart.plotBorderColor,
  376. 'stroke-width': 1
  377. })
  378. .add();
  379. }
  380. };
  381. /**
  382. * Get the title based on the XML data
  383. */
  384. Meteogram.prototype.getTitle = function () {
  385. return this.xml.location.name + ', ' + this.xml.location.country;
  386. };
  387. /**
  388. * Build and return the Highcharts options structure
  389. */
  390. Meteogram.prototype.getChartOptions = function () {
  391. var meteogram = this;
  392. return {
  393. chart: {
  394. renderTo: this.container,
  395. backgroundColor:'transparent',
  396. marginBottom: 70,
  397. marginRight: 40,
  398. marginTop: 50,
  399. plotBorderWidth: 1,
  400. // width: 800,
  401. // height: 310
  402. width: this.getWidth(),
  403. height: this.getHeight()
  404. },
  405. title: {
  406. text: this.getTitle(),
  407. align: 'left'
  408. },
  409. credits: {
  410. text: '',
  411. href: '#'
  412. },
  413. tooltip: {
  414. shared: true,
  415. useHTML: true,
  416. formatter: function () {
  417. return meteogram.tooltipFormatter(this);
  418. }
  419. },
  420. xAxis: [{ // Bottom X axis
  421. type: 'datetime',
  422. tickInterval: 2 * 36e5, // two hours
  423. minorTickInterval: 36e5, // one hour
  424. tickLength: 0,
  425. gridLineWidth: 1,
  426. gridLineColor: (Highcharts.theme && Highcharts.theme.background2) || '#F0F0F0',
  427. startOnTick: false,
  428. endOnTick: false,
  429. minPadding: 0,
  430. maxPadding: 0,
  431. offset: 30,
  432. showLastLabel: true,
  433. labels: {
  434. format: '{value:%H}'
  435. }
  436. }, { // Top X axis
  437. linkedTo: 0,
  438. type: 'datetime',
  439. tickInterval: 24 * 3600 * 1000,
  440. labels: {
  441. format: '{value:<span style="font-size: 12px; font-weight: bold">%a</span> %b %e}',
  442. align: 'left',
  443. x: 3,
  444. y: -5
  445. },
  446. opposite: true,
  447. tickLength: 20,
  448. gridLineWidth: 1
  449. }],
  450. yAxis: [{ // temperature axis
  451. title: {
  452. text: null
  453. },
  454. labels: {
  455. format: '{value}°',
  456. style: {
  457. fontSize: '10px'
  458. },
  459. y: -2,
  460. x: -3
  461. },
  462. plotLines: [{ // zero plane
  463. value: 0,
  464. color: '#BBBBBB',
  465. width: 1,
  466. zIndex: 2
  467. }],
  468. // Custom positioner to provide even temperature ticks from top down
  469. tickPositioner: function () {
  470. var max = Math.ceil(this.max) + 1,
  471. pos = max - 12, // start
  472. ret;
  473. if (pos < this.min) {
  474. ret = [];
  475. while (pos <= max) {
  476. ret.push(pos += 1);
  477. }
  478. } // else return undefined and go auto
  479. return ret;
  480. },
  481. maxPadding: 0.3,
  482. tickInterval: 1,
  483. gridLineColor: (Highcharts.theme && Highcharts.theme.background2) || '#F0F0F0'
  484. }, { // precipitation axis
  485. title: {
  486. text: null
  487. },
  488. labels: {
  489. enabled: false
  490. },
  491. gridLineWidth: 0,
  492. tickLength: 0
  493. }, { // Air pressure
  494. allowDecimals: false,
  495. title: { // Title on top of axis
  496. text: 'hPa',
  497. offset: 0,
  498. align: 'high',
  499. rotation: 0,
  500. style: {
  501. fontSize: '10px',
  502. color: Highcharts.getOptions().colors[1]
  503. },
  504. textAlign: 'left',
  505. x: 3,
  506. y: -5
  507. },
  508. labels: {
  509. style: {
  510. fontSize: '10px',
  511. color: Highcharts.getOptions().colors[1]
  512. },
  513. y: -2,
  514. x: 3
  515. },
  516. gridLineWidth: 0,
  517. opposite: true,
  518. showLastLabel: false
  519. }],
  520. legend: {
  521. enabled: false
  522. },
  523. plotOptions: {
  524. series: {
  525. pointPlacement: 'between'
  526. }
  527. },
  528. series: [{
  529. name: 'Temperature',
  530. data: this.temperatures,
  531. type: 'spline',
  532. marker: {
  533. enabled: false,
  534. states: {
  535. hover: {
  536. enabled: true
  537. }
  538. }
  539. },
  540. tooltip: {
  541. valueSuffix: '°C'
  542. },
  543. zIndex: 1,
  544. color: Highcharts.getOptions().colors[0] || '#3d9dc7',
  545. negativeColor: '#48AFE8'
  546. }, {
  547. name: 'Precipitation',
  548. data: this.precipitations,
  549. type: 'column',
  550. color: Highcharts.getOptions().colors[2] || '#68CFE8',
  551. yAxis: 1,
  552. groupPadding: 0,
  553. pointPadding: 0,
  554. borderWidth: 0,
  555. shadow: false,
  556. dataLabels: {
  557. enabled: true,
  558. formatter: function () {
  559. if (this.y > 0) {
  560. return this.y;
  561. }
  562. },
  563. style: {
  564. fontSize: '8px'
  565. }
  566. },
  567. tooltip: {
  568. valueSuffix: 'mm'
  569. }
  570. }, {
  571. name: 'Air pressure',
  572. color: Highcharts.getOptions().colors[1],
  573. data: this.pressures,
  574. marker: {
  575. enabled: false
  576. },
  577. shadow: false,
  578. tooltip: {
  579. valueSuffix: ' hPa'
  580. },
  581. dashStyle: 'shortdot',
  582. yAxis: 2
  583. }]
  584. }
  585. };
  586. /**
  587. * Post-process the chart from the callback function, the second argument to Highcharts.Chart.
  588. */
  589. Meteogram.prototype.onChartLoad = function (chart) {
  590. this.drawWeatherSymbols(chart);
  591. this.drawWindArrows(chart);
  592. this.drawBlocksForWindArrows(chart);
  593. };
  594. /**
  595. * Create the chart. This function is called async when the data file is loaded and parsed.
  596. */
  597. Meteogram.prototype.createChart = function () {
  598. var meteogram = this;
  599. this.chart = new Highcharts.Chart(this.getChartOptions(), function (chart) {
  600. meteogram.onChartLoad(chart);
  601. });
  602. };
  603. /**
  604. * Handle the data. This part of the code is not Highcharts specific, but deals with yr.no's
  605. * specific data format
  606. */
  607. Meteogram.prototype.parseYrData = function () {
  608. var meteogram = this,
  609. xml = this.xml,
  610. pointStart;
  611. if (!xml || !xml.forecast) {
  612. $('#loading').html('<i class="fa fa-frown-o"></i> Failed loading data, please try again later');
  613. return;
  614. }
  615. // The returned xml variable is a JavaScript representation of the provided XML,
  616. // generated on the server by running PHP simple_load_xml and converting it to
  617. // JavaScript by json_encode.
  618. $.each(xml.forecast.tabular.time, function (i, time) {
  619. // Get the times - only Safari can't parse ISO8601 so we need to do some replacements
  620. var from = time['@attributes'].from + ' UTC',
  621. to = time['@attributes'].to + ' UTC';
  622. from = from.replace(/-/g, '/').replace('T', ' ');
  623. from = Date.parse(from);
  624. to = to.replace(/-/g, '/').replace('T', ' ');
  625. to = Date.parse(to);
  626. if (to > pointStart + 4 * 24 * 36e5) {
  627. return;
  628. }
  629. // If it is more than an hour between points, show all symbols
  630. if (i === 0) {
  631. meteogram.resolution = to - from;
  632. }
  633. // Populate the parallel arrays
  634. meteogram.symbols.push(time.symbol['@attributes']['var'].match(/[0-9]{2}[dnm]?/)[0]);
  635. meteogram.symbolNames.push(time.symbol['@attributes'].name);
  636. meteogram.temperatures.push({
  637. x: from,
  638. y: parseInt(time.temperature['@attributes'].value),
  639. // custom options used in the tooltip formatter
  640. to: to,
  641. index: i
  642. });
  643. meteogram.precipitations.push({
  644. x: from,
  645. y: parseFloat(time.precipitation['@attributes'].value)
  646. });
  647. meteogram.windDirections.push(parseFloat(time.windDirection['@attributes'].deg));
  648. meteogram.windDirectionNames.push(time.windDirection['@attributes'].name);
  649. meteogram.windSpeeds.push(parseFloat(time.windSpeed['@attributes'].mps));
  650. meteogram.windSpeedNames.push(time.windSpeed['@attributes'].name);
  651. meteogram.pressures.push({
  652. x: from,
  653. y: parseFloat(time.pressure['@attributes'].value)
  654. });
  655. if (i == 0) {
  656. pointStart = (from + to) / 2;
  657. }
  658. });
  659. // Smooth the line
  660. this.smoothLine(this.temperatures);
  661. // Create the chart when the data is loaded
  662. this.createChart();
  663. };
  664. // End of the Meteogram protype