0

in my Rails application I have two views that produces charts based on flot charts and this post

Now, these two pages are different in only plotNames array.

This is obviously a bad implementation, so I wanted to know what would be the best way of sharing the relevant parts between the pages?

As mentioned, other than that I have two coffescript files looking like that:

    colorArray = []
colorArray.push "rgba(180, 0, 75,    0.6)"
colorArray.push "rgba(0, 150, 100,   0.6)"
colorArray.push "rgba(0, 0, 255,     0.6)"
colorArray.push "rgba(140, 0, 255,   0.6)"
colorArray.push "rgba(90, 180, 20,   0.6)"
colorArray.push "rgba(255, 236, 0,   0.6)"
colorArray.push "rgba(234, 170, 21,  0.6)"
colorArray.push "rgba(95, 180, 190,  0.6)"
colorArray.push "rgba(214, 92, 63,   0.6)"
colorArray.push "rgba(218, 106, 234, 0.6)"
colorArray.push "rgba(213, 128, 155, 0.6)"

# chart colors default 
$chrt_border_color = "#efefef"
$chrt_grid_color = "#DDD"
$chrt_main = "#E24913"

# red       
$chrt_second = "#6595b4"
# blue      
$chrt_third = "#FF9F01"
# orange    
$chrt_fourth = "#7e9d3a"
# green     
$chrt_fifth = "#BD362F"
# dark red  
$chrt_mono = "#000"

Chart = 

    generateDataObjects: (all_series, all_series_data) ->
        plotData = []

        for series, i in all_series
            obj =
                label: series.replace /__/g, "|"
                data: all_series_data[i]
                color: colorArray[i]

            # obj = (
            #   label: series
            #   console.log "pushing series #{series}"
            #   data: all_series_data[i]
            #   color: colorArray[i]
            #   console.log "pushing color #{color} to #{series} series"
            #   )
            plotData.push obj

        return plotData

    togglePlot: (plotName, seriesIdx, totalNumOfSeries) ->
        console.log "seriesIdx is: #{seriesIdx}"
        someData = this.plot[plotName].getData()
        someData[seriesIdx-totalNumOfSeries].lines.show = not someData[seriesIdx-totalNumOfSeries].lines.show
        someData[seriesIdx-totalNumOfSeries].points.show = not someData[seriesIdx-totalNumOfSeries].points.show
        this.plot[plotName].setData someData
        this.plot[plotName].draw()
        return

    getTooltip: (label, xval, yval, flotItem) ->
        return '<span class="label bg-color-teal txt-color-white">'+label+'</span>'+'<br>Build: <span>'+ flotItem.series.data[flotItem.dataIndex][2]+'</span>' +"<br>Run ID: <strong> #{flotItem.series.data[flotItem.dataIndex][3].toString()}</strong>" + '<br>Result: <span>'+Chart.commify(yval)+'</span>'

    commify: (x) ->
        return x.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",");

    generateChartOptions: (legend_container, ticks) ->
        this.legendindex = 0
        return (
            series:
                lines:
                    show: true

                points:
                    show: true

            crosshair:
                mode: "x"
                color: "#FF9900"

            legend:
                container: $("##{legend_container}")
                labelFormatter: (label, series) ->
                    "<a href=\"javascript:void(0);\" class=\"legendtoggle\" data-index=\"" + Chart.legendindex++ + "\">" + label + "</a>"
                # labelFormatter: (label, series) ->
    #                   "<a href=\"javascript:void(0);\" onClick=\"Chart.togglePlot(" + series.idx + "); return false;\">" + label + "</a>"
                noColumns: 4
                # hideable: true

            grid:
              hoverable: true
              clickable: true
              tickColor: $chrt_border_color
              borderWidth: 0
              borderColor: $chrt_border_color

            tooltip: true
            tooltipOpts: 
              content : Chart.getTooltip 
              #content : "Value <b>$x</b> Value <span>$y</span>",
              defaultTheme: false

            xaxis:
                ticks: ticks
                rotateTicks: 30

            selection:
                mode: "xy"
            )

    plot: {}

plotNames = ["bandwidth", "normalized_bw", "concurrent_flows"]


jQuery ->
    jQuery.each plotNames, (index, name) ->
        if $("#"+name+"_chart").length
            console.log "handling #{name}_chart"
            raw_data = $("#"+name+"_chart").data('results')

            ticks = $("#"+name+"_chart").data('ticks')
            all_series = $("#"+name+"_chart").data('series')
            console.log "total # of series is #{all_series.length}"

            Chart.plot[name] = $.plot($("#"+name+"_chart"), Chart.generateDataObjects(all_series, raw_data), Chart.generateChartOptions(name+"_legend", ticks))

            $("#"+name+"_legend").on 'click', 'a.legendtoggle', (event) ->
                Chart.togglePlot(name, $(this).data('index'), all_series.length)
                return false

            $("#"+name+"_chart").bind "plotselected", (event, ranges) ->
                Chart.plot[name] = $.plot($("#"+name+"_chart"), Chart.plot[name].getData(), $.extend(true, {}, Chart.generateChartOptions(name+'_legend', ticks),
                  xaxis:
                    min: ranges.xaxis.from
                    max: ranges.xaxis.to
                  yaxis:
                    min: ranges.yaxis.from
                    max: ranges.yaxis.to
                ))
                return

            $("#"+name+"_chart").bind "dblclick", (event, pos, item) ->
                Chart.plot[name] = $.plot($("#"+name+"_chart"), Chart.plot[name].getData(), $.extend(true, {}, Chart.generateChartOptions(name+'_legend', ticks),
                  xaxis:
                    min: null
                    max: null
                  yaxis:
                    min: null
                    max: null
                ))
                return
Community
  • 1
  • 1
cyber101
  • 899
  • 1
  • 9
  • 19
  • Throw that code somewhere in `assets/javascripts/` and load the data through an AJAX call or embed it right in the page I guess. – mu is too short Oct 13 '14 at 18:15
  • I'm afraid that this is too general. Also, I'm sure that a sharing without Ajax call is possible, but I don't know hot to achieve this. – cyber101 Oct 14 '14 at 04:15
  • A way (not sure if the cleanest one) would be to add .erb to your filename and to move the common code into a partial which will be included where you need it. This will take more time on precompile but it would be a DRY approach – SDp Oct 17 '14 at 00:26

0 Answers0