Jump to content

{resolved} Chart on the dashboard


Recommended Posts

Hello, is it possible to change the colors of the dashboard graph which represents the sales statistics?

Currently we have 2 years and the color red and blue. I would like to have already 3 or 4 years instead of 2. And red for 2020, blue for 2019, and a dark gray for 2018 and a light gray for 2017

Link to comment
Share on other sites

We can work on getting more years added after verifying the colors work.

In dashboard.index.inc.php, find:

    $chart_data['title'] = $lang['dashboard']['title_sales_stats'].': '.$last_year.' - '.$this_year;
    $GLOBALS['smarty']->assign('CHART', $chart_data);

On a blank line ABOVE that, add this:

$chart_data['series'] = "[{color:'black'},{color:'yellow'}]";


In the skin template dashboard.index.php, find:

           var options = {
             title: '{/literal}{$CHART.title}{literal}',

On a new blank line AFTER that, add this:

series: {/literal}{$CHART.series}{literal},

Change the name of the colors (or #html_color_code)  to correlate with the specific year.

Link to comment
Share on other sites

I finished making the graph show more years. The changes to the code were numerous, but minor. It would be best to replace the whole section that calculates the chart data.

In the admin, /sources/dashboard.index.inc.php, near line 185, find:

## Statistics (Google Charts)

Then, near line 213, find:

$GLOBALS['smarty']->assign('CHART', $chart_data);

The following will REPLACE EVERYTHING between those two lines, including those two lines:

    ## Statistics (Google Charts)
    $no_years_to_show = 4;
    $chart_data['series'] = "[{color:'black'},{color:'yellow'},{color:'green'},{color:'purple'}]";

    $this_year = date('Y');
    $chart_data['title'] = $lang['dashboard']['title_sales_stats'].': ' . ($this_year +1 - $no_years_to_show) . ' - '.$this_year;
    $first_year_start = mktime(0, 0, 0, '01', '01', $this_year +1 - $no_years_to_show);
    $sales = $GLOBALS['db']->select('CubeCart_order_summary', array('order_date', 'total'), array('order_date' => '>='.$first_year_start, 'status' => array(2, 3), 'total' => '>0'));
    $data= array();
    if ($sales) { ## Get data to put in chart
        foreach ($sales as $sale) {
            $year = date('Y', $sale['order_date']);
            $month = date('M', $sale['order_date']);
            if (isset($data[$year][$month])) {
                $data[$year][$month] += sprintf('%0.2f', $sale['total']);
            } else {
                $data[$year][$month] = sprintf('%0.2f', $sale['total']);
            }
        }
    }

    $chart_data['data'] = "['Month', ";
    for ($i = 0; $i < $no_years_to_show; ++$i) {
        $this_year_offset = $this_year - $i;
        $chart_data['data'] .= "'".$this_year_offset."',";
    }
    $chart_data['data'] .= "],";
    for ($month = 1; $month <= 12; $month++) {
        $m = date("M", mktime(0, 0, 0, $month, 10));
        $chart_data['data'] .= "['$m',";
        for ($i = 0; $i < $no_years_to_show; ++$i) {
            $year_month = (isset($data[$this_year - $i][$m]) && $data[$this_year - $i][$m]>0) ? $data[$this_year - $i][$m] : 0;
            $chart_data['data'] .= "$year_month,";
        }
        $chart_data['data'] .= "],";
    }

    $GLOBALS['smarty']->assign('CHART', $chart_data);

Note the first two lines after ## Statistics (Google Charts). Here you can set the number of calendar years to include in the chart (which includes the current year). And also be able to specify what colors to use for the current year, then last year, then the year before that,, etc, etc, then the oldest year.

 

Link to comment
Share on other sites

  • fabriceunko changed the title to {resolved} Chart on the dashboard

In the admin skin template:

Find:

var chart = new google.visualization.LineChart(document.getElementById('chart_div'));

Change to:

var chart = new google.visualization.ColumnChart(document.getElementById('chart_div'));

The vertical scale? That's automatic.

The abbreviated names of the months? We will need to find the way for PHP to use locale-specific results in some of the instances of the date() function.

Try this. In the admin /source/dashboard.index.inc.php, find:

global $glob, $lang, $admin_data;

Add after:

setlocale(LC_TIME, "designation");

where designation is something like fr-FR for French.

We need to find the very specific instances of the function date() and replace that with strftime(). There may be some tweaking involved. Be back later.

Link to comment
Share on other sites

In dashboard.index.inc.php:

Find:

$chart_data['data'] .= "['$m',";

Change to:

$chart_data['data'] .= "['".setlocale(LC_TIME,"fr_FR.UTF-8", "fr_FR","fr").strftime("%B", mktime(0, 0, 0, $month, 10))."',";

I didn't get French names probably because I do not have a French library installed for PHP nor for the operating system of my web server.

Revert back to LineChart and see what happens.

Link to comment
Share on other sites

Having setlocale() elsewhere, make this edit:

Find:

$chart_data['data'] .= "['".setlocale(LC_TIME,"fr_FR.UTF-8", "fr_FR","fr").strftime("%B", mktime(0, 0, 0, $month, 10))."',";

Change to:

$chart_data['data'] .= "['".strftime("%B", mktime(0, 0, 0, $month, 10))."',";

 

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...