I am having issues getting views charts to work properly with the date module.

Let's say i would like a monthly report on sales and use a custom date format of month-year such as September-2009.

I use the SQL Aggregation (group by fields) and group by the date field.

i would prefer to use the function sum, but let's go with count for now...

Aggregate on the sale price (since were counting rather than summing, will count # of prices)

Sort by Date

I do get a chart, but each bar is 1 because charts is not using the custom date format of month-year. In other words, each date is it's own X-axis bar, thus always having a count of 1.

It would be great if Charts could take the same Date format as the Date field in the view, thus when grouping on a date all September-2009 items become aggregated together.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

irakli’s picture

Category: bug » feature
tobiberlin’s picture

I found a similar problem as I wanted to have an x-axis with dates but they are shown in YYYY-MM-DDT00:00:00 format - can I change the format somewhere?

Greez,
Tobias

irakli’s picture

Tobias,

not yet, but this seems to be a high-demand request, so we converted it to a feature request and will try to implement for the next release.

Thank you.

patrickfrickel’s picture

Ah ha....This was exactly the issue I was having...It is one of the biggest points of interest for business....sales by date, visitors by date, issues by date, issue resolution by date, # of issues per month comparison, etc, etc...I'm a bit amazed that there are graphs that don't use dates as a criteria....possibly I have been working on business reporting for to long :)

sammyman’s picture

I would also love to see this feature. Amcharts and this module are great, but that date is too long.

waltercat’s picture

Jumping on the date support band wagon too!

Negs’s picture

+1 on date formatting options... in the meantime though, is there a workaround by going into the code and changing a function or variable or something?

Cyberwolf’s picture

Subscribing.

deggertsen’s picture

I'm surprised that there hasn't been more interest in this... Not being able to format dates on a chart seems like a pretty big deal to me. Any progress here?

Subscribing.

rsevero’s picture

Project: Views Charts » Views Group By

Changing it to Views GroupBy as there is a aggregation issue that must be dealt by Views GroupBy: in Views you only format the output, not the data as it's seen by the SQL query. This affects dates and all other data types.

So even if you set views to show date as Y-m-d for example, you still don't get aggregated data as the data as seen by the SQL has hour, minute and second which differ so don't end aggregated.

Formatting the date in the output works and Views Charts respects it so there is no issue in Views Charts.

sammys’s picture

FileSize
4.25 KB

I've patched views_groupby to make some of this possible. The problem: aggregation needs to occur at query level and the views date handler keeps the values in 2010-09-17 00:00:00 format. If you want to group by 2010-09-17 you're screwed.

The patch adds a new handler that gets used instead views_handler_field_date for all date fields. It's ok because it's backwards compatible. :)

The handler adds a new setting to the field: Aggregate format. This setting allows you to set the format used in the database query rather than in the rendering. You can then properly use the value as a grouping field. You can also still use the rendering format settings as you could previously.

NOTE: You can't get something from nothing. If your aggregate format removes hours you can't use hours in the rendered format.

Hope this saves someone's day.

sammys’s picture

FileSize
4.25 KB

Updated to be better. You need a patch for views_charts to bring the render format into play. Find that patch at:
#914654: Support field rewriting and special aliasing for labels

obrienmd’s picture

sammys: I added your patch, but can't seem to find the "Aggregate format" setting in any of my date fields. Any advice?

kevinob11’s picture

Subscribe

5t4rdu5t’s picture

In my case, I needed to count records grouped by month of node created date. Here's a quick workaround solution that I've found on some forum using hook_views_pre_execute in a custom module:

<?php

function mymodule_views_pre_execute(&$view) {
  if ($view->name == 'MyViewName' && $view->current_display == 'myCurrentDisplay') {
    $search = array('GROUP BY node_created, term_data_name');
    $replace = array('GROUP BY YEAR(FROM_UNIXTIME(node_created)) + MONTH(FROM_UNIXTIME(node_created))');
    $view->build_info['query'] = str_replace($search, $replace, $view->build_info['query']);
    $view->build_info['count_query'] = str_replace($search, $replace, $view->build_info['count_query']);
  }
}

?>

This basically replaces the query's GROUP BY clause with the one that worked for me. This may help someone get things working until a better solution is implemented for date formats.

podox’s picture

Status: Active » Needs work
FileSize
40.97 KB

obrienmd: The Aggregate format setting is at the bottom of any date field in Views. See attached screenshot.

My problem is that this setting is not persisting. I enter m Y in the Aggregate format box, hit update, but if I try to edit the field again, the Aggregate format box is empty. The charts still use the standard Drupal date format (identified in the setting directly above the Aggregate format box, in my case D, d/m/Y - H:i)

I'm using Views 3, Views Group By 6.x-1.x-dev and used the patch in #12. The patch needed to Views Charts has already been committed in the latest version.

obrienmd’s picture

podox: Doesn't views 3 have built-in groupby?

podox’s picture

Status: Needs work » Needs review

Gah - yes, it does. I hadn't explored that. I guess Views GroupBy isn't designed (or necessary) for Views 3 and that may well be the source of my non-persisting field error.

FWIW, the built-in groupby appears to have the same limitation on Date field formats - it also uses the full timestamp.

Changing status...

Leon Kessler’s picture

Great patch, thanks Sammys.

The only thing I would say is the description for the Aggregation format textfield says strings for the PHP date function are accepted. When actually the views_date_sql_format() function that it passes through only accepts Y-m-d-H-i-s.

mpavankumar’s picture

I need to group by 1st of every month like group by March 1, 2011; April 1, 2011; May 1, 2011 etc...Can that be achieved?

akleinwaechter’s picture

The patch is great. How could it be modified to work with CCK Datetime fields?

Best regards.
Alex

Omio’s picture

I need this patch to work with Views 2. Can anybody help me?

jvieille’s picture

The patch #12 works great in D6, Views 2.
Don't forget to save the page module to register the new handler.
Nothing appears on the aggregation field, the setting is in the date field itself.

jvieille’s picture

Issue summary: View changes
Status: Needs review » Reviewed & tested by the community

It seems that this worked for everyone who tested it.

marinex’s picture

@podox:
Hi you must change function in the patch 573264_12.patch:

  function option_definition() {
    $options = parent::option_definition();

    $options['format_query'] = array('default' => '');
    
    return $options;
  } 

to

  function option_definition() {
    $options = parent::option_definition();

    $options['query_format'] = array('default' => '');
    
    return $options;
  }

because there is typo error: from format_query to query_format
I am using this module with views 3.x and it working good.