Ero sivun ”Widget:XJwCharts” versioiden välillä
Järvi-meriwikistä
(24 välissä olevaa versiota samalta käyttäjältä ei näytetä) | |||
Rivi 22: | Rivi 22: | ||
jQuery( '.jwChartWidget' ).each( function() { | jQuery( '.jwChartWidget' ).each( function() { | ||
− | var memo = { | + | var memo = { 'now': new Date() }; |
− | |||
memo.widgetEl = jQuery( this ); | memo.widgetEl = jQuery( this ); | ||
− | if ( typeof memo.widgetEl.prop( 'id' ) == 'undefined' ) { memo.widgetEl.prop( 'id', 'jwChartWidget_' + now.getTime() ); } | + | if ( typeof memo.widgetEl.prop( 'id' ) == 'undefined' ) { memo.widgetEl.prop( 'id', 'jwChartWidget_' + memo.now.getTime() ); } |
memo.widgetEl.css( 'position', 'relative' ); | memo.widgetEl.css( 'position', 'relative' ); | ||
memo.lang = memo.widgetEl.attr( 'data-lang' ); if (typeof memo.lang == 'undefined') { memo.lang = 'fi'; } | memo.lang = memo.widgetEl.attr( 'data-lang' ); if (typeof memo.lang == 'undefined') { memo.lang = 'fi'; } | ||
− | + | memo.obscode = memo.widgetEl.attr( 'data-obscode' ); if (typeof memo.obscode == 'undefined') { memo.lang = 'ice'; }; | |
− | + | memo.siteid = memo.widgetEl.attr( 'data-siteid' ); //'284245'; //'240829'; //'284245'; | |
− | memo.siteid = memo.widgetEl.attr( 'data | ||
// add chart container | // add chart container | ||
memo.widgetEl.append( '<div class="jwChartContainer" style="position: absolute; z-index: 110; width: 100%; height: 100%;"></div>' ); | memo.widgetEl.append( '<div class="jwChartContainer" style="position: absolute; z-index: 110; width: 100%; height: 100%;"></div>' ); | ||
− | memo.chartEl = | + | memo.chartEl = memo.widgetEl.find( '.jwChartContainer' ); |
− | memo.widgetEl.append( '<div class="jwloading"></div>' ); | + | memo.widgetEl.append( '<div style="display: none;" class="jwloading"></div>' ); |
− | + | memo.loadingEl = memo.widgetEl.find( '.jwloading' ); | |
− | + | ||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
// add fullscreen button | // add fullscreen button | ||
//memo.widgetEl.append( '<div class="obschartbutton fullscreen-button" style="Xdisplay: none; position: absolute; z-index: 120; float: right; top: 40px; right: 1.5px; background-color: rgba( 255, 255, 255, .9 ); border-radius: 50%; border: 1px solid rgba( 230, 230, 230, 0.75 ); width: 24px; height: 24px; padding: 0;"><button title="fullscreen" style="width: 24px; height: 24px; padding: 0; margin: 0; background: none; border: none; background-image: url( //www.jarviwiki.fi/widgets/img/fullscreen_16px.png ); background-size: 50%; background-position: center; background-repeat: no-repeat; "></button></div>' ); | //memo.widgetEl.append( '<div class="obschartbutton fullscreen-button" style="Xdisplay: none; position: absolute; z-index: 120; float: right; top: 40px; right: 1.5px; background-color: rgba( 255, 255, 255, .9 ); border-radius: 50%; border: 1px solid rgba( 230, 230, 230, 0.75 ); width: 24px; height: 24px; padding: 0;"><button title="fullscreen" style="width: 24px; height: 24px; padding: 0; margin: 0; background: none; border: none; background-image: url( //www.jarviwiki.fi/widgets/img/fullscreen_16px.png ); background-size: 50%; background-position: center; background-repeat: no-repeat; "></button></div>' ); | ||
Rivi 59: | Rivi 50: | ||
memo.jw = new jwApi(); | memo.jw = new jwApi(); | ||
+ | memo.loadingEl.show( 'slow' ); | ||
+ | |||
memo.jw.loadObses( { 'obscode': memo.obscode, 'siteid': memo.siteid, 'custom': '[[Jäätilanne::2||5]]' }, function( o ) { | memo.jw.loadObses( { 'obscode': memo.obscode, 'siteid': memo.siteid, 'custom': '[[Jäätilanne::2||5]]' }, function( o ) { | ||
+ | obsesReady( memo ); | ||
+ | }); | ||
+ | |||
+ | |||
+ | function obsesReady( memo ) { | ||
+ | |||
+ | // margin settings | ||
+ | memo.margins = { top: 0, right: 25, bottom: 60, left: 60 }; | ||
+ | |||
+ | if ( typeof i18n[ memo.obscode ].direction != 'undefined' ) { memo.vaxisdir = i18n[ memo.obscode ].direction; } else { memo.vaxisdir = 1; } | ||
+ | |||
+ | // defaults for all charts | ||
+ | memo.options = { | ||
+ | title: i18n[memo.obscode]['title'][memo.lang], | ||
+ | //titleTextStyle: { fontSize: 14 }, | ||
+ | backgroundColor: 'transparent', | ||
+ | chartArea: { top: memo.margins.top, left: memo.margins.left, width: (memo.widgetEl.width() - memo.margins.left - memo.margins.right), height: (memo.widgetEl.height() - memo.margins.top - memo.margins.bottom), backgroundColor: { fill: '#daf0fa', stroke: '#fff', strokeWidth: 6 }}, | ||
+ | legend: { position: 'bottom' }, | ||
+ | crosshair: { trigger: 'both' }, | ||
+ | tooltip: { isHtml: true }, | ||
+ | seriesType: 'scatter', | ||
+ | interpolateNulls: true, | ||
+ | |||
+ | vAxis: { | ||
+ | title: i18n[memo.obscode]['axisTitleV'][memo.lang], | ||
+ | titleTextStyle: { bold: true }, | ||
+ | slantedText: false, | ||
+ | maxAlternation: 1, | ||
+ | direction: memo.vaxisdir, | ||
+ | gridlines: { color: '#fff' }, | ||
+ | minorGridlines: { count: 0 }, | ||
+ | viewWindow: { } | ||
+ | }, | ||
+ | hAxis: { | ||
+ | title: '', | ||
+ | slantedText: false, | ||
+ | maxAlternation: 1, | ||
+ | gridlines: { color: '#fff' }, | ||
+ | minorGridlines: { count: 0 }, | ||
+ | viewWindow: { } | ||
+ | }, | ||
+ | series: [ ], | ||
+ | trendlines: [ ] | ||
+ | }; | ||
+ | |||
+ | memo.obsesTotal = 0; | ||
+ | memo.obsMinValue = Infinity; | ||
+ | memo.obsMaxValue = -Infinity; | ||
+ | memo.obsMinDateMs = Infinity; | ||
+ | memo.obsMaxDateMs = -Infinity; | ||
+ | memo.obsMinCompDateMs = Infinity; | ||
+ | memo.obsMaxCompDateMs = -Infinity; | ||
+ | |||
+ | memo.season = i18n[memo.obscode]['season']; | ||
+ | memo.curSeason = memo.now.getFullYear(); | ||
+ | if ( memo.season == 'winter' && memo.now.getMonth() < 8 ) { memo.curSeason--; } | ||
+ | |||
+ | // first loop | ||
+ | for ( var o in memo.jw.obs ) { | ||
+ | |||
+ | obs = memo.jw.obs[o]; | ||
+ | |||
+ | console.log( obs ); | ||
+ | |||
+ | memo.obsesTotal++; | ||
+ | |||
+ | memo.obsMinValue = Math.min( memo.obsMinValue, obs.value ); | ||
+ | memo.obsMaxValue = Math.max( memo.obsMaxValue, obs.value ); | ||
+ | |||
+ | memo.obsMinDateMs = Math.min( memo.obsMinDateMs, obs.obsdatetime.getTime() ); | ||
+ | memo.obsMaxDateMs = Math.max( memo.obsMaxDateMs, obs.obsdatetime.getTime() ); | ||
+ | |||
+ | // add compare date to obses | ||
+ | if ( memo.season == 'summer' || obs.obsdatetime.getMonth() > 7 ) { | ||
+ | obs.compareDate = new Date( memo.curSeason, obs.obsdatetime.getMonth(), obs.obsdatetime.getDate() ); | ||
+ | } else { | ||
+ | obs.compareDate = new Date( memo.curSeason+1, obs.obsdatetime.getMonth(), obs.obsdatetime.getDate() ); | ||
+ | } | ||
+ | ms = obs.compareDate.getTime(); | ||
+ | memo.obsMinCompDateMs = Math.min(ms, memo.obsMinCompDateMs); | ||
+ | memo.obsMaxCompDateMs = Math.max(ms, memo.obsMaxCompDateMs); | ||
+ | |||
+ | // add series id | ||
+ | if ( memo.graphType == 'trend' ) { | ||
+ | obs.seriesId = 0; | ||
+ | } else if ( memo.season == 'summer' || obs.obsdatetime.getMonth() > 7 ) { | ||
+ | obs.seriesId = obs.obsdatetime.getFullYear(); | ||
+ | } else { | ||
+ | obs.seriesId = obs.obsdatetime.getFullYear()-1; | ||
+ | } | ||
+ | |||
+ | // create popup for obs | ||
+ | obs.popup = '<div style="padding: 8px;">'; | ||
+ | obs.popup += '<div>'; | ||
+ | |||
+ | if ( i18n[memo.obscode]['type'] == 'cat') { | ||
+ | obs.popup +='<b>' + i18n[memo.obscode]['title'][memo.lang] + ': ' + i18n[memo.obscode]['catNames'][obs.value][memo.lang] + '</b> '; | ||
+ | } else if ( i18n[memo.obscode]['type'] == 'date') { | ||
+ | obs.popup +='<b>' + i18n[memo.obscode]['title'][memo.lang] + '</b> '; | ||
+ | } else { | ||
+ | obs.popup +='<b>' + i18n[memo.obscode]['title'][memo.lang] + ': ' + (Math.round(obs.value * 10)/10).toString().replace( '.', i18n['dec'][memo.lang] ) + ' ' + i18n[memo.obscode]['unit'] + '</b> '; | ||
+ | } | ||
+ | |||
+ | obs.popup += '(' + obs.obsdatetime.inRelationToToday(memo.lang) + ')'; | ||
+ | obs.popup += '</div>'; | ||
+ | |||
+ | if (!!obs.addInfo) { obs.popup += '<div style="margin-top: 5px;">' + obs.addInfo + '</div>'; } | ||
+ | obs.popup += '</div>'; | ||
+ | |||
+ | } | ||
+ | |||
+ | memo.obsMinDate = new Date( memo.obsMinDateMs ); | ||
+ | memo.obsMaxDate = new Date( memo.obsMaxDateMs ); | ||
+ | memo.obsMinYear = memo.obsMinDate.getFullYear(); | ||
+ | memo.obsMaxYear = memo.obsMaxDate.getFullYear(); | ||
+ | memo.obsMinCompDate = new Date( memo.obsMinCompDateMs ); | ||
+ | memo.obsMaxCompDate = new Date( memo.obsMaxCompDateMs ); | ||
+ | |||
+ | memo.obsesA = []; memo.dataA = []; | ||
+ | |||
+ | for (var o in memo.jw.obs) { | ||
+ | |||
+ | obs = memo.jw.obs[o]; | ||
+ | |||
+ | // style points | ||
+ | if ( obs.seriesId == memo.curSeason ) { | ||
+ | baseSize = 6; | ||
+ | } else if (memo.obsesTotal < 100) { | ||
+ | baseSize = 4; | ||
+ | } else if (memo.obsesTotal < 200) { | ||
+ | baseSize = 3; | ||
+ | } else if (memo.obsesTotal < 300) { | ||
+ | baseSize = 2; | ||
+ | } else { | ||
+ | baseSize = 2; | ||
+ | } | ||
+ | |||
+ | //if ( memo.graphType == 'dateofyear' ) { baseSize = baseSize + 1; } | ||
+ | |||
+ | if (!!obs.addInfo) { | ||
+ | //obs.style = 'point { shape-type: star; size: ' + (baseSize+3) + '; shape-dent: 0.5; fill-color: ' + colors['noticeObses'] + '; stroke-width: 0; stroke-color: ' + maintColors[memo.obses[o].maintainer] + '; }'; | ||
+ | //obs.style = 'point { size: ' + (baseSize+1) + '; stroke-width: 2; stroke-color: gold; }'; | ||
+ | obs.style = 'point { size: ' + (baseSize+1) + '; stroke-width: 3; stroke-color: ' + maintColors[obs.maintainer] + '; }'; | ||
+ | } else { | ||
+ | obs.style = 'point { size: ' + baseSize + '; stroke-width: 0.5; stroke-color: ' + maintColors[obs.maintainer] + '; }'; | ||
+ | } | ||
+ | |||
+ | memo.obsesA.push( obs ); | ||
+ | |||
+ | } | ||
+ | |||
+ | // sort | ||
+ | memo.obsesA.sort( function(a, b) { return a.obsdatetime - b.obsdatetime; }); | ||
+ | |||
+ | // create DataTable | ||
+ | memo.data = new google.visualization.DataTable(); | ||
+ | |||
+ | // col for xAxis values | ||
+ | memo.data.addColumn( 'date', 'time' ); | ||
+ | |||
+ | // make ticks for y axis on cat types | ||
+ | if ( i18n[memo.obscode]['type'] == 'cat' ) { | ||
+ | memo.options.vAxis.ticks = []; | ||
+ | for (var c in i18n[memo.obscode]['catNamesShort']) { | ||
+ | memo.options.vAxis.ticks.push({v: parseInt(c), f: i18n[memo.obscode]['catNamesShort'][c][memo.lang] }); | ||
+ | memo.options.vAxis.ticks.push({v: parseInt(c)+0.5, f: '' }); | ||
+ | } | ||
+ | memo.options.vAxis.ticks.sort( function(a, b) { return a.v-b.v } ); | ||
+ | memo.options.vAxis.viewWindow = { min: -0.5, max: parseInt(memo.options.vAxis.ticks[memo.options.vAxis.ticks.length-1].v)+0.5 }; | ||
+ | memo.options.vAxis.gridlines.color = '#fff'; | ||
+ | memo.options.vAxis.baselineColor = 'none'; | ||
+ | } else if ( typeof i18n[memo.obscode]['min'] != 'undefined' ) { | ||
+ | memo.options.vAxis.viewWindow = { min: i18n[memo.obscode]['min'] }; | ||
+ | } else if ( memo.obsMinValue >= 0) { | ||
+ | memo.options.vAxis.viewWindow = { min: 0 }; | ||
+ | } | ||
+ | |||
+ | if (memo.graphType == 'trend') { | ||
+ | |||
+ | //addDataToTrendChart(memo); | ||
+ | |||
+ | } else if (memo.graphType == 'season') { | ||
+ | |||
+ | //addDataToSeasonChart(memo); | ||
+ | |||
+ | } else if (memo.graphType == 'dateofyear') { | ||
+ | |||
+ | //addDataToDateOfYearChart(memo); | ||
+ | |||
+ | } | ||
data = {}; | data = {}; | ||
var firstW = 3000; lastW = 0; | var firstW = 3000; lastW = 0; | ||
var firstF = 500; var lastB = -500; | var firstF = 500; var lastB = -500; | ||
+ | //var firstCd = new Date( memo.now.getFullyear(), 11, 31 ); var lastCd = new Date(); | ||
− | var cnewyear = new Date( now.getFullYear()+1, 0, 1 ); | + | var cnewyear = new Date( memo.now.getFullYear()+1, 0, 1 ); |
for ( var o in memo.jw.obsList ) { | for ( var o in memo.jw.obsList ) { | ||
Rivi 74: | Rivi 258: | ||
if ( obs.obsdatetime.getMonth() > -1 && obs.obsdatetime.getMonth() < 8 ) { | if ( obs.obsdatetime.getMonth() > -1 && obs.obsdatetime.getMonth() < 8 ) { | ||
obs.winter = obs.obsdatetime.getFullYear()-1; | obs.winter = obs.obsdatetime.getFullYear()-1; | ||
− | obs.comparedate = new Date( now.getFullYear()+1, obs.obsdatetime.getMonth(), obs.obsdatetime.getDate() ); | + | obs.comparedate = new Date( memo.now.getFullYear()+1, obs.obsdatetime.getMonth(), obs.obsdatetime.getDate() ); |
} else { | } else { | ||
obs.winter = obs.obsdatetime.getFullYear(); | obs.winter = obs.obsdatetime.getFullYear(); | ||
− | obs.comparedate = new Date( now.getFullYear(), obs.obsdatetime.getMonth(), obs.obsdatetime.getDate() ); | + | obs.comparedate = new Date( memo.now.getFullYear(), obs.obsdatetime.getMonth(), obs.obsdatetime.getDate() ); |
} | } | ||
Rivi 143: | Rivi 327: | ||
row = [ y, null, null, null, null, null, null, null, null, null ]; | row = [ y, null, null, null, null, null, null, null, null, null ]; | ||
} | } | ||
− | |||
− | |||
table.push( row ); | table.push( row ); | ||
Rivi 170: | Rivi 352: | ||
seriesType: "candlesticks", | seriesType: "candlesticks", | ||
series: { 1: { type: "scatter", color: '#3366cc', dataOpacity: 0.5 }, 2: { type: "scatter", color: '#3366cc', dataOpacity: 0.5 } }, | series: { 1: { type: "scatter", color: '#3366cc', dataOpacity: 0.5 }, 2: { type: "scatter", color: '#3366cc', dataOpacity: 0.5 } }, | ||
− | trendlines: { 1: { type: ' | + | trendlines: { 1: { type: 'linear', color: 'orange' }, 2: { type: 'linear', color: 'orange' } }, |
legend: 'bottom', | legend: 'bottom', | ||
hAxis: { slantedText: false, maxAlternation: 1, format: '####' }, | hAxis: { slantedText: false, maxAlternation: 1, format: '####' }, | ||
Rivi 188: | Rivi 370: | ||
doResize(); | doResize(); | ||
− | } | + | } // ObsesReady |
+ | |||
+ | function addDataToDateOfYearChart( memo ) { | ||
+ | |||
+ | // do X axis min and max for this chart type | ||
+ | memo.options.hAxis.minValue = new Date( memo.obsMinDateMs ); | ||
+ | memo.options.hAxis.minValue.setMonth(0); | ||
+ | memo.options.hAxis.minValue.setDate(1); | ||
+ | memo.options.hAxis.viewWindow.min = memo.options.hAxis.minValue; | ||
+ | |||
+ | memo.options.hAxis.maxValue = new Date( memo.obsMaxDateMs ); | ||
+ | memo.options.hAxis.maxValue.setMonth(11); | ||
+ | memo.options.hAxis.maxValue.setDate(31); | ||
+ | memo.options.hAxis.viewWindow.max = memo.options.hAxis.maxValue; | ||
+ | |||
+ | memo.options.vAxis.ticks = []; | ||
+ | |||
+ | //memo.options.hAxis.gridlines.units = { years: { format: [ 'yyyy' ] } }; | ||
+ | //memo.options.hAxis.ticks = []; | ||
+ | |||
+ | // do Y axis min and max for this chart type | ||
+ | memo.options.vAxis.minValue = new Date( memo.obsMinCompDate.getTime() ); | ||
+ | memo.options.vAxis.minValue.setDate(1); | ||
+ | memo.options.vAxis.viewWindow.min = memo.options.vAxis.minValue; | ||
+ | |||
+ | memo.options.vAxis.maxValue = new Date( memo.obsMaxCompDate.getTime() ); | ||
+ | memo.options.vAxis.maxValue.setMonth( memo.options.vAxis.maxValue.getMonth()+1 ); | ||
+ | memo.options.vAxis.maxValue.setDate(0); | ||
+ | memo.options.vAxis.viewWindow.max = memo.options.vAxis.maxValue; | ||
+ | |||
+ | // do Y axis ticks and gridlines | ||
+ | memo.options.vAxis.gridlines = { color: 'transparent' }; | ||
+ | minM = memo.options.vAxis.minValue.getMonth(); | ||
+ | maxM = memo.options.vAxis.maxValue.getMonth(); | ||
+ | if (minM > maxM) { mc = 13-minM+maxM; } else { mc = maxM-minM+1; } | ||
+ | |||
+ | for ( mi = 0; mi < mc; mi++ ) { | ||
+ | |||
+ | mx = minM+mi; | ||
+ | yx = memo.options.vAxis.minValue.getFullYear(); | ||
+ | if ( mx > 11 ) { yx = maxD.getFullYear(); mx = mx - 12; } | ||
+ | |||
+ | startOfMonth = new Date( yx, mx, 1 ); | ||
+ | middleOfMonth = new Date( yx, mx, 15 ); | ||
+ | |||
+ | // ticks | ||
+ | memo.options.vAxis.ticks.push( { v: middleOfMonth, f: i18n['months'][mx]['long'][memo.lang] } ); | ||
+ | |||
+ | // gridlines except for first | ||
+ | memo.vAxisGridlines = []; | ||
+ | if (mi > 0) { | ||
+ | memo.vAxisGridlines.push( new Date( startOfMonth.getTime() ) ); | ||
+ | } | ||
+ | } | ||
+ | |||
+ | // data columns | ||
+ | memo.data.addColumn( 'date', memo.obsMinYear + '-' + memo.obsMaxYear ); | ||
+ | memo.data.addColumn( {'type': 'string', 'role': 'style' } ); | ||
+ | memo.data.addColumn( { type: 'string', role: 'annotation' } ); | ||
+ | memo.data.addColumn( { type: 'string', role: 'tooltip', 'p': { 'html': true } } ); | ||
+ | |||
+ | // scatter series | ||
+ | memo.options.series[0] = { type: 'scatter', pointType: 'circle', pointSize: 9, color: '#3366cc' }; | ||
+ | |||
+ | // linear trend line | ||
+ | memo.options.trendlines[0] = { labelInLegend: i18n['trendline'][memo.lang], tooltip: false, color: colors['trend'], lineWidth: 2, opacity: 0.5, type: 'linear', visibleInLegend: memo.trendType!='moving' }; | ||
+ | |||
+ | // data columns for vAxis gridlines | ||
+ | for (g=0; g < memo.vAxisGridlines.length; g++) { | ||
+ | memo.data.addColumn( 'date', 'vAxis gridline_' + g ); | ||
+ | memo.options.series[g+1] = { type: 'line', pointSize: 0, lineWidth: 1, color: '#fff', XlineDashStyle: [3, 3], visibleInLegend: false }; | ||
+ | } | ||
+ | |||
+ | // add data | ||
+ | noc = memo.data.getNumberOfColumns(); | ||
+ | |||
+ | // add obses | ||
+ | for (var o in memo.obsesA) { | ||
+ | dataRow = []; | ||
+ | |||
+ | // fill row with nulls | ||
+ | for (var i=0; i<noc; i++) { dataRow.push( null ); } | ||
+ | |||
+ | centerOfYear = new Date( memo.obsesA[o].datetime.getTime() ); | ||
+ | centerOfYear.setMonth(6); | ||
+ | centerOfYear.setDate(1); | ||
+ | |||
+ | dataRow[0] = centerOfYear; | ||
+ | dataRow[1] = memo.obsesA[o].compareDate; | ||
+ | dataRow[2] = memo.obsesA[o].style; | ||
+ | // annotation | ||
+ | dataRow[4] = memo.obsesA[o].popup; | ||
+ | |||
+ | memo.dataA.push( dataRow ); | ||
+ | } | ||
+ | |||
+ | // add gridlines | ||
+ | for (g=0; g < memo.vAxisGridlines.length; g++) { | ||
+ | |||
+ | dataRow1 = []; | ||
+ | dataRow2 = []; | ||
+ | |||
+ | // fill rows with nulls | ||
+ | for (var i=0; i<noc; i++) { dataRow1.push( null ); dataRow2.push( null ); } | ||
+ | |||
+ | dataRow1[0] = memo.options.hAxis.minValue; | ||
+ | dataRow2[0] = memo.options.hAxis.maxValue; | ||
+ | |||
+ | dataRow1[5+g] = memo.vAxisGridlines[g]; | ||
+ | dataRow2[5+g] = memo.vAxisGridlines[g]; | ||
+ | |||
+ | memo.dataA.push( dataRow1 ); | ||
+ | memo.dataA.push( dataRow2 ); | ||
+ | } | ||
+ | |||
+ | drawChart(memo); | ||
+ | |||
+ | } | ||
memo.fullscreenEl.on( 'click', function() { | memo.fullscreenEl.on( 'click', function() { |