highcharts-datalet.html 6.07 KB
<!--
@license
    The MIT License (MIT)

    Copyright (c) 2015 Dipartimento di Informatica - Universit� di Salerno - Italy

    Permission is hereby granted, free of charge, to any person obtaining a copy
    of this software and associated documentation files (the "Software"), to deal
    in the Software without restriction, including without limitation the rights
    to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
    copies of the Software, and to permit persons to whom the Software is
    furnished to do so, subject to the following conditions:

    The above copyright notice and this permission notice shall be included in
    all copies or substantial portions of the Software.

    THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
    THE SOFTWARE.
-->

<!--
* Developed by :
* ROUTE-TO-PA Project - grant No 645860. - www.routetopa.eu
*
-->

<link rel="import" href="../base-ajax-json-jsonpath-datalet/base-ajax-json-jsonpath-datalet.html">

<!--
The `highcharts-datalet` uses the base-ajax-json-jsonpath-datalet as data retriving and selection mechanism. It defines the common properties
for all the highchart datalets : categories, series and series_type. Overmore override the transformData method
to create and populate categories and series array. Every specific highchart chart implementation (eg linechart, barchart, columnchart ...)
will use highcharts-datalet properties to create the appropriate chart.

Example :

    <dom-module id="highcarts-chart-datalet">
        <template>
            ...
            <highcharts-datalet id="charts" data-url="{{dataUrl}}" fields="{{fields}}"></highcharts-datalet>
            ...
        </template>
    </dom-module>

@element highcharts-datalet
@status v0.1
@homepage
@group datalets
-->

<dom-module id="highcharts-datalet">

    <template>
        <div id="container" style="width:auto; height:auto;"></div>
        <base-ajax-json-jsonpath-datalet data-url="{{dataUrl}}" fields="{{fields}}" data="{{data}}" title="{{title}}" description="{{description}}"></base-ajax-json-jsonpath-datalet>
    </template>

    <script src="static/js/highcharts.js"></script>
    <script src="static/js/exporting.js"></script>

    <script>
        var HighchartsBehavior = {

            properties: {

                categories: {
                    type: Array,
                    value: []
                },

                series: {
                    type: Array,
                    value: []
                },

                series_type:{
                    type: String,
                    value: "line"//spline,time
                }

            },
            /**
             * Normalizes a number in agreement with javascript's conventions. Delete all NaN characters. Exception: number representing lat & long remain unchanged.
             */
            jNumConverter: function(num) {
                //lat-long
                if(num.charAt(num.length-7) == "." && (num.match(/[\.,]/g) || []).length == 1)
                    return num;

                var jNum = "";
                for (var i = 0; i < num.length; i++) {
                    if(num[i].match(/[\.,]/g))
                        if (i == num.length - 3)
                            jNum += ".";
                        else
                            ;
                    else if (!isNaN(num[i]))
                        jNum += num[i];
                }

                return jNum;
            },
            /**
             * Populate the categories and the series array.
             *
             * @method transformData
             *
             * @param {Event} e
             */
            transformData: function () {

                if(this.data.length == 0) return;

                this.properties.categories.value = this.data[0].data;

                for (var i = 1; i < this.data.length; i++)
                {

                    this.data[i].data.every(function (element, index, array) {
                        try {
                            var e = HighchartsBehavior.jNumConverter(element);
                            (isNaN(element)) ? array[index] = parseFloat(HighchartsBehavior.jNumConverter(element)) :
                                    array[index] = parseFloat(element);
                        }catch(e){
                            //console.log("Parsing data error. Highchart-datalet.selectData");
                        }
                        return true;
                    });

                    this.properties.series.value.push(this.data[i]);
                }
            },

            setParameters: function(params)
            {
                this._component.title = params['title'];
                this._component.description = params['description'];

                var chart = $(this._component.$.charts.$.container).highcharts();

                chart.setTitle({text: params['title']});
                chart.xAxis[0].setTitle({text: params['x-axis-label']});
                chart.yAxis[0].setTitle({text: params['y-axis-label']});

                chart.tooltip.options.formatter = function() {
                    return this.x + '<br/><br/><span style="fill:#7cb5ec" x="8" dy="15">●</span> ' + this.series.name + ': <b>' + this.y + ' ' + params['suffix'] + '</b>';
                }

            }

        };

        var HighchartsComponentBehavior = $.extend(true, {}, BaseDataletBehavior, WorkcycleBehavior, AjaxJsonJsonPathBehavior, HighchartsBehavior);

        HighchartsDatalet = Polymer({
            is : 'highcharts-datalet'
        });
    </script>

</dom-module>