highcharts-datalet.html 5.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-alasql-datalet/base-ajax-json-alasql-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-alasql-datalet data-url="{{dataUrl}}" fields="{{fields}}" data="{{data}}" title="{{title}}" description="{{description}}" export_menu="{{export_menu}}"></base-ajax-json-alasql-datalet>
    </template>

    <script src="static/js/highcharts.js"></script>
    <script src="static/js/highcharts-more.js"></script>
    <script src="static/themes/themes.js"></script>

    <script>
        var HighchartsBehavior = {

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

            transformData: function () {
                var selectedFields = JSON.parse(this._component.getAttribute("selectedFields"));

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

                categories = this.data[0].data;
                series = [];
                for(var i = 1; i < this.data.length; i++)
                    series.push(this.data[i]);

                this.properties.categories.value = categories;
                this.properties.series.value = series;

                if(this.data.length == 3 && !selectedFields){//split cat
                    var x = this.data[0]["data"];
                    var gb = this.data[1]["data"];
                    var y = this.data[2]["data"];

                    var categories = x.filter(function(item, pos) {
                        return x.indexOf(item) == pos;
                    })

                    var s = gb.filter(function(item, pos) {
                        return gb.indexOf(item) == pos;
                    })

                    var series = [];
                    for(var i = 0; i < s.length; i++) {
                        series.push({name: s[i], data: new Array(categories.length+1).join('0').split('').map(parseFloat)});
                    }

                    for(var i = 0; i < y.length; i++){
                        var index = categories.indexOf(x[i]);
                        var s = series.filter(function( obj ) {
                            return obj.name == gb[i];
                        });
                        s[0]["data"][index] = y[i];
                    }

                    this.properties.categories.value = categories;
                    this.properties.series.value = series;
                }

            }

        };

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

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

</dom-module>