Tuesday, May 3, 2011

Flex: How to add horizontal axes to a chart?

I have a dynamically created chart and I need to add a horizontal and vertical axis in a script. I can't put the chart into MXML because I'm dynamically changing the chart type, deleting and creating a new chart. Therefore, the variable that I use to create the chart, an instance of chartBase, doesn't have either a horizontalAxis or verticalAxis. How do I assign to them if they don't exist? Should I be using a different type? The graphs I want to use are line, bar, and HLOC and/or candlestick.

From stackoverflow
  • I think you'll get better results by instantiating your chart as a proper type.

    For example, in a component I have a skeleton lineChart setup in the mxml...

    <mx:LineChart id="lineChart" dataProvider="{chartData}" dataTipFunction="dataTipFunction"
     width="100%"
     height="100%"
     showDataTips="true"
     />
    

    ... but you could create it as a variable and add it to the container. Then for the axis I determine the type I want and create and apply them, here's some code I use for the horizontal axis...

    // Setup new horizontal axis.
    switch (hAxisFieldsComboBox.selectedItem.dataType)
    {
     case "Date":
      var hDtAxis:DateTimeAxis = new DateTimeAxis();
      hDtAxis.title = hAxisFieldsComboBox.selectedItem.label;
      hDtAxis.dataUnits = "days";
      hDtAxis.dataInterval = 1;
      hDtAxis.parseFunction = dateParser;
      lineChart.horizontalAxis = hDtAxis;
      break;
     case "Time":
      var hTAxis:DateTimeAxis = new DateTimeAxis();
      hTAxis.title = hAxisFieldsComboBox.selectedItem.label;
      hTAxis.dataInterval = 1;
      hTAxis.parseFunction = timeParser;
      lineChart.horizontalAxis = hTAxis;
      break;
     case "Number":
      var hLAxis:LinearAxis = new LinearAxis();
      hLAxis.title = hAxisFieldsComboBox.selectedItem.label;
      hLAxis.interval = 1;
      hLAxis.minimum = 0;
      lineChart.horizontalAxis = hLAxis;
      break;
     default:
      var hCatAxis:CategoryAxis = new CategoryAxis();
      hCatAxis.title = hAxisFieldsComboBox.selectedItem.label;
      hCatAxis.dataProvider = schemaUtil.aggregateAndSortCasesAC(chartData, vAxisFieldsComboBox.selectedItem.data, [hAxisFieldsComboBox.selectedItem.data]);
      hCatAxis.categoryField = hAxisFieldsComboBox.selectedItem.data;
      lineChart.horizontalAxis = hCatAxis;
    }
    

    Hope that helps.

0 comments:

Post a Comment