Brandon Lara
Brandon Lara

Reputation: 3

Javafx chart change NumberAxis to CategoryAxis

I have created an AreaChart with SceneBuilder with NumberAxis on X axis and NumberAxis on Y axis.

@FXML
private  AreaChart<Number,Number> areaChart;

and FXML:

<AreaChart fx:id="areaChart" alternativeColumnFillVisible="true" createSymbols="false" style="-fx-horizontal-grid-lines-color: white;" title="Altura durante la sesión">
    <xAxis>
        <NumberAxis fx:id="areaX" animated="false" label="Distancia (metros)"    style="axis_color: white;" />
    </xAxis>
    <yAxis>
        <NumberAxis fx:id="areaY" label="Altura (metros)" style="axis_color:    white;" />
    </yAxis>

I want by pressing a button convert that areaChart to an AreaChart with a NumberAxis on Y axis and a CategoryAxis on X axis. because I have to make a Heigth x Distance chart and then by pressing a button convert that chart to Height x Time chart and the result after pressing the button has to be this:

<AreaChart fx:id="areaChart" alternativeColumnFillVisible="true" createSymbols="false" style="-fx-horizontal-grid-lines-color: white;" title="Altura durante la sesión">
    <xAxis>
        <CategoryAxis fx:id="areaX" animated="false" label="Distancia (metros)"    style="axis_color: white;" />
    </xAxis>
    <yAxis>
        <NumberAxis fx:id="areaY" label="Altura (metros)" style="axis_color:    white;" />
    </yAxis>

Upvotes: 0

Views: 1925

Answers (1)

jewelsea
jewelsea

Reputation: 159290

I don't think it is a good idea to change try to change the axis type of a chart (I'm not even sure if you can do that).

Instead, I advise:

  1. Creating two charts.
  2. For one use a CategoryAxis and for the other use a NumberAxis.
  3. Add similar data (but probably different) series to both charts.
  4. Put both charts in a StackPane.
  5. Add a ToggleButton, ToggleSwitch, CheckBox or RadioButton (some kind of toggle control), to switch between the two states.
  6. Bind the visibility to one chart to the selected state of the toggle and bind the visibility of the other chart to not the selected state of the toggle.

That should allow you to display the appropriate chart with the appropriate axis type on demand.

Upvotes: 1

Related Questions