package EPrints::Plugin::Stats::View::Google::PieChart; use EPrints::Plugin::Stats::View; @ISA = ('EPrints::Plugin::Stats::View'); use strict; # Stats::View::Google::PieChart # # Works in a similar way that View::Table except that it renders the data on a pie chart, generated by Google Charts sub javascript_class { return 'GooglePieChart'; } sub mimetype { my( $self ) = @_; return "application/json"; } sub render_title { my( $self ) = @_; my $context = $self->context; my $grouping = defined $context->{grouping} ? ":".$context->{grouping} : ""; return $self->html_phrase( "title$grouping" ); } # same as Stats::View::Table sub get_data { my( $self ) = @_; my $session = $self->{session}; # We need to know the Top we're going to display... if( !EPrints::Utils::is_set( $self->options->{top} ) ) { print "Stats::View::Google::PieChart: missing option 'top'\n"; return $session->make_doc_fragment; } # This bit of code tries to map what the user wants to view given the context my $options = $self->options; $options->{do_render} = ( defined $options->{export} ) ? 0 : 1; $options->{limit} ||= 10; delete $options->{limit} if( $options->{limit} eq 'all' ); my $top = $self->options->{top}; # Perhaps the user wants to see the top: # - eprints # - eg top authors # - eg top referrers / country... if( $top eq 'eprint' ) { # we need to fetch eprint objects ie 'eprintid' $self->context->{grouping} = 'eprint'; $options->{fields} = [ 'eprintid' ]; } elsif( $top eq $self->context->{datatype} ) { $self->context->{grouping} = 'value'; $options->{fields} = [ 'value' ]; } elsif( EPrints::Utils::is_set( $self->context->{set_name} ) ) { $self->context->{grouping} = $top; $options->{fields} = [ 'set_value' ]; } else { # perhaps it's a set then... let's assume so! $self->context->{set_name} = $top; delete $self->context->{grouping}; $options->{fields} = [ 'set_value' ]; } $self->{options} = $options; return $self->handler->data( $self->context )->select( %$options ); } sub render_content_ajax { my( $self ) = @_; my $session = $self->{session}; my $stats = $self->get_data; my $options = $self->options; my @full_labels; foreach( @{$stats->data} ) { my $object = $_->{$options->{fields}->[0]}; my $count = $_->{count}; push @full_labels, "[ \"".EPrints::XML::to_string( $object )."\", ".$count."]"; } my $jsdata = join(",",@full_labels); print STDOUT "{ \"data\": [$jsdata] }"; return; } 1;