This discussion is archived
13 Replies Latest reply: Nov 23, 2012 7:37 PM by James_D RSS

JavaFx 2.x: BarChart with Numer, Number

935521 Newbie
Currently Being Moderated
I need to have a BarChart with Number, Number axis but it seems it is not possibile and I must always have Category, Number axis.

Is there a way to override BarChart class so to get both axis as Number, Number?

If not, is it possibile to use LineChart to plot bars instead of BarChat class?

Thanks

Edited by: 932518 on 29-set-2012 11.33
  • 1. Re: JavaFx 2.x: BarChart with Numer, Number
    James_D Guru
    Currently Being Moderated
    Bar charts are necessarily categorical: one axis has a finite collection of discrete values (the bars). If you happen to have these labeled numerically, just use a CategoryAxis where the values are the String versions of the numbers.
  • 2. Re: JavaFx 2.x: BarChart with Numer, Number
    908082 Newbie
    Currently Being Moderated
    This is a bug/limitation specific to Java FX charts. You should file a bug report.
  • 3. Re: JavaFx 2.x: BarChart with Numer, Number
    935521 Newbie
    Currently Being Moderated
    I have tried this solution
    XYChart.Series series = new XYChart.Series();                        
            series.getData().add(new XYChart.Data(i, i));
            series.getData().add(new XYChart.Data(i, listValue));            
            lineChart.getData().addAll(series);
    but it is very slow compared to a x axis Number plot.

    Is there a way to plot it faster?
  • 4. Re: JavaFx 2.x: BarChart with Numer, Number
    935521 Newbie
    Currently Being Moderated
    I did modify the code as: in a cycle I've done something like:
    XYChart.Series series = new XYChart.Series();
    
       for ( i ... traverse data list  ) {
       series.getData().add(new XYChart.Data(i, i));
       series.getData().add(new XYChart.Data(i, listValue));            
       series.getData().add(new XYChart.Data(i+1, listValue));
      }
      lineChart.getData().addAll(series);
    but using linechart I get empty bars , how can I fill these bars?
  • 5. Re: JavaFx 2.x: BarChart with Numer, Number
    James_D Guru
    Currently Being Moderated
    It's not completely clear to me what you're trying to do here, but would using an AreaChart instead of a LineChart get you what you want?
  • 6. Re: JavaFx 2.x: BarChart with Numer, Number
    935521 Newbie
    Currently Being Moderated
    Hi James, thanks for your suggestion: Area Chart may suit my needs, but how to remove the dots plotted on the line(s) of the Area Chart?
  • 7. Re: JavaFx 2.x: BarChart with Numer, Number
    James_D Guru
    Currently Being Moderated
    Not sure. I would try setting the Node property on the series, or using css.
  • 8. Re: JavaFx 2.x: BarChart with Numer, Number
    jsmith Guru
    Currently Being Moderated
    how to remove the dots plotted on the line(s) of the Area Chart?
    See: "Thread: How to remove symbols from an AreaChart in JavaFX"
    How to remove symbols from an AreaChart in JavaFX

    METHOD 1: load a stylesheet which styles makes the chart symbols invisible.
    METHOD 2: set the node for a data point to something invisible (an empty rectangle).
    METHOD 3: lookup the symbols node via css and make them invisible.
    METHOD 4: subclass the chart and override the chart's dataItemAdded method to remove symbols on each data point

    And, as with everything in JavaFX, there are probably five other ways to do it which I missed . . .
  • 9. Re: JavaFx 2.x: BarChart with Numer, Number
    935521 Newbie
    Currently Being Moderated
    Thank you very much for your help.
  • 10. Re: JavaFx 2.x: BarChart with Numer, Number
    929085 Newbie
    Currently Being Moderated
    I don't think so. What about histograms you obtain from a frequency distribution. They could best displayed as <Number, Number> especially if they had some zero values between the y-axis values and you want to keep them in between. You, can convert Number to String to satisfy the CategoryAxis requirement of JavaFX BarChart, but this is not the only restriction with JavaFX bar charts. What about an real-time calculated histogram that should immediately updates itself. This is not unusual in the age of streaming data. In this case JavaFx BarChart is shiftless. Because, if you have a frequency distribution that you dynamically update, its numerical y-axis should be ideally a sorted Set, not a List; since you insert these values only once and then update their frequencies. And you do this operation ideally using a sorted-set. Now ask yourself how can you update the correspoinding JavaFx chart under these conditions. Although it provides ObservableList to serve self updating charts, it provides a list not a sorted-set. Converting a sorted set to a list, requires inserting into the list, searching it linearly to find the right order. As such, it should be clear that generic charts should not be restricted in terms of their axis, or data structures. Ideal BarChart should be declared like its parent XYChart namely BarChart<Axis<X>, Axis<Y>>, and ideal XYChart should not only work with ObservableLists but also with sets, or maps. Especially Map is nothing else than a function. Which we you can plot as such.
  • 11. Re: JavaFx 2.x: BarChart with Numer, Number
    James_D Guru
    Currently Being Moderated
    A histogram is not a bar chart, though they are visually similar. See for example http://en.m.wikipedia.org/wiki/Bar_chart. A bar chart by definition displays categorical data; a histogram displays an approximation to a frequency distribution. JavaFX doesn't currently have a Histogram class; one would be a good idea. Histogram would be a subclass of XYChart<Number,Number> as you suggest.
  • 12. Re: JavaFx 2.x: BarChart with Numer, Number
    935521 Newbie
    Currently Being Moderated
    Thanks for all replies,

    is it possible to have a sample code of a sublcass <Number, Number> so to plot as histogram?
  • 13. Re: JavaFx 2.x: BarChart with Numer, Number
    James_D Guru
    Currently Being Moderated
    That would actually be a bit of a project... I'd find it interesting but I'm unlikely to have time to try it any time soon.

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points