重构为Ruby on Rails重复HighChart配置

时间:2014-01-09 14:48:16

标签: javascript jquery ruby-on-rails

我正在使用HighChart:api.highcharts.com/插件在我的Ruby on Rails应用程序中支持和处理我的折线图。

我在4个不同的js.erb文件中有4个不同的折线图。

除了计算的数据外,所有图表都具有相同的样式/配置。

     var seriesOptions = [],
                yAxisOptions = [],
                seriesCounter = 0,
                names = <%= raw get_engagement_data(@user,params[:provider],params[:type]) %>;

        $.each(names, function(i, name) {

            seriesOptions[i] = {
                type: 'areaspline',
                lineColor: getColor[name[2]],
                lineWidth: 2,
                pointInterval: 24 * 3600 * 1000,
                name: name[0],
                data: name[1],
                fillColor: getLighterColor[name[2]],
                fillOpacity: 1,
                marker: {
                    enabled: false,
                    fillColor: getColor[name[2]]
                }
            };

            // As we're loading the data asynchronously, we don't know what order it will arrive. So
            // we keep a counter and create the chart when all the data is loaded.
            seriesCounter++;

            if (seriesCounter == names.length) {
                createChart();
            }
        });

        // create the chart when all data is loaded
        function createChart() {

            $('#engagement_chart').highcharts('StockChart', {
                chart: {
                },

                rangeSelector: {
                    enabled: false
                },
                scrollbar: {
                    enabled: false
                },
                credits: {
                    enabled: false
                },
                navigator: {
                    maskFill: '#3ebca6',
                    height: 10,
                    margin: 25,
                    outlineColor: '#3ebca6',
                    outlineWidth: 5,
                    series: {
                        color: '#3ebca6'
                    },
                    xAxis: {
                       labels: {
                            enabled: false
                        }
                    }
                },
                xAxis: {
                    type: 'datetime',
                    dateTimeLabelFormats: {
                        second: '%b<br>%Y',
                            minute: '%b<br>%Y',
                        hour: '%b<br>%Y',
                        day: '%b %e<br>%Y',
                        week: '%b<br>%Y',
                        month: '%b<br>%Y',
                        year: '%Y'
                    },
                    labels: {
                        style: {
                            color: '#27303a',
                            fontFamily: 'Helvetica Neue',
                            fontSize: '12px'
                        }
                    }
                },
                yAxis: {
                    min: 0,
                    startOnTick: true,
                    gridLineColor: '#f0f0f1',
                    gridLineWidth: "1px",
                       offset: 60,
                    labels: {
                        style: {
                            color: '#27303a',
                            fontFamily: 'Helvetica Neue',
                            fontSize: '12px'
                        }
                    }
                },
                plotOptions: {
                    padding: "10px",
                    series: {
                        fillOpacity: 1
                    }
                },

                tooltip: {
                    pointFormat: '<span style="color:{series.color}">{series.name}</span>: <b>{point.y}</b><br/>'
                },

                series: seriesOptions
            });
        }

我尝试创建单独的/assets/javascripts/graph.js文件

graph.js

       function ColorTheDonuts(){
         chart: {
                },

                rangeSelector: {
                    enabled: false
                },
                scrollbar: {
                    enabled: false
                },
                credits: {
                    enabled: false
                },
                navigator: {
                    maskFill: '#3ebca6',
                    height: 10,
                    margin: 25,
                    outlineColor: '#3ebca6',
                    outlineWidth: 5,
                    series: {
                        color: '#3ebca6'
                    },
                    xAxis: {
                       labels: {
                            enabled: false
                        }
                    }
                },
                xAxis: {
                    type: 'datetime',
                    dateTimeLabelFormats: {
                        second: '%b<br>%Y',
                            minute: '%b<br>%Y',
                        hour: '%b<br>%Y',
                        day: '%b %e<br>%Y',
                        week: '%b<br>%Y',
                        month: '%b<br>%Y',
                        year: '%Y'
                    },
                    labels: {
                        style: {
                            color: '#27303a',
                            fontFamily: 'Helvetica Neue',
                            fontSize: '12px'
                        }
                    }
                },
                yAxis: {
                    min: 0,
                    startOnTick: true,
                    gridLineColor: '#f0f0f1',
                    gridLineWidth: "1px",
                       offset: 60,
                    labels: {
                        style: {
                            color: '#27303a',
                            fontFamily: 'Helvetica Neue',
                            fontSize: '12px'
                        }
                    }
                },
                plotOptions: {
                    padding: "10px",
                    series: {
                        fillOpacity: 1
                    }
                },
        }

我把一些配置放在那里,但JS没有用。

有什么建议吗?任何变通办法将不胜感激。感谢

1 个答案:

答案 0 :(得分:0)

你很亲密。 ColorTheDonuts函数中的JavaScript语法不正确:

function ColorTheDonuts(){
    return {
        chart: {},
        rangeSelector: {
            enabled: false
        },
        scrollbar: {
            enabled: false
        },
        credits: {
            enabled: false
        },
        navigator: {
            maskFill: '#3ebca6',
            height: 10,
            margin: 25,
            outlineColor: '#3ebca6',
            outlineWidth: 5,
            series: {
                color: '#3ebca6'
            },
            xAxis: {
                labels: {
                    enabled: false
                }
            }
        },
        xAxis: {
            type: 'datetime',
            dateTimeLabelFormats: {
                second: '%b<br>%Y',
                minute: '%b<br>%Y',
                hour: '%b<br>%Y',
                day: '%b %e<br>%Y',
                week: '%b<br>%Y',
                month: '%b<br>%Y',
                year: '%Y'
            },
            labels: {
                style: {
                    color: '#27303a',
                    fontFamily: 'Helvetica Neue',
                    fontSize: '12px'
                }
            }
        },
        yAxis: {
            min: 0,
            startOnTick: true,
            gridLineColor: '#f0f0f1',
            gridLineWidth: "1px",
            offset: 60,
            labels: {
                style: {
                    color: '#27303a',
                    fontFamily: 'Helvetica Neue',
                    fontSize: '12px'
                }
            }
        },
        plotOptions: {
            padding: "10px",
            series: {
                fillOpacity: 1
            }
        }
    };
}

使用:

$('#engagement_chart').highcharts('StockChart', ColorTheDonuts());