+ Start a Discussion

How to create a chart with a variable number of series?



I have what I thought would be a simple thing but can’t work out how, or if, Visualforce charts can do it.


I have a set of data derived in Apex code which has:


  1. User Name
  2. Category
  3. Number (in category)

… and would like to graph it as such (using X, Y, Z to represent colour bars):


  5                             Legend
N 4   XY         Z              User X
u 3   XY         Z      Y       User Y
m 2   XYZ       YZ     XY       User Z
  1   XYZ      XYZ     XYZ
      Good     Bad     Ugly

The problem I have is that both the number of users and number of categories is not fixed (they are determined in the controller's Apex code). I can ensure every User has a value for every category (even if it is 0) so that is not a problem. In the sparse examples in the docs, I can only see how to do it when the number of categories is indetermined. They show data structures as such:


graph data {








But since I don't know how many users I'll have (nor their names) until the code has run, I am not sure how to put the data nor the visualforce code together. I tried experimenting with things like:


<apex:chart height="300" width="700" data="{!activityGraph}" id="actChart">
    <apex:legend position="right"/>
    <apex:axis type="Numeric" position="left" fields="numberM" title="# Ms"/>
    <apex:axis type="Category" position="bottom" fields="catLabel" />
    <apex:barSeries title="{!activityGraph[0].userName}" axis="left" xField="catLabel" yField="{!activityGraph[0].numberM}" orientation="vertical"/>

…with the hope of embedding the barSeries into a <apex:repeat> loop. But that doesn't work.


I am really hoping that I am missing something simple here. Anyone got any ideas?




Sonali BhardwajSonali Bhardwaj

I think you need to fix either category or user. Basically category is your data which you want to plot in chart and that needs to be fixed. You need to create a wrapper class which will be kind of:

class myData {

string user;

Integer goodCategoryData;

Integer badCategoryData;

Integer uglyCategoryData;




If nothing is fixed, you can create a matrix report on your object which store all the information and you can include chart in that. Chart with report should be able to implement your requirement.


This set of requirement can be possibly acheived by Google charts, Try out this http://www.salesforce.com/us/developer/docs/pages/Content/pages_quick_start_advanced_google_charts.htm


Thank you both for trying to help. It is surprising that something you can do in the in-built report charts can't be done in the "more flexible" visualforce charts.