java - Error trying to put dates on Xaxis in a line chart -
package org.gillius.jfxutils.examples; import javafx.animation.animation; import javafx.animation.keyframe; import javafx.animation.timeline; import javafx.application.application; import javafx.event.actionevent; import javafx.event.eventhandler; import javafx.fxml.fxml; import javafx.fxml.fxmlloader; import javafx.scene.scene; import javafx.scene.chart.linechart; import javafx.scene.chart.xychart; import javafx.scene.control.label; import javafx.scene.control.slider; import javafx.scene.input.mousebutton; import javafx.scene.input.mouseevent; import javafx.scene.layout.region; import javafx.scene.layout.stackpane; import javafx.stage.stage; import javafx.util.duration; import org.gillius.jfxutils.jfxutil; import org.gillius.jfxutils.chart.chartpanmanager; import org.gillius.jfxutils.chart.fixedformattickformatter; import org.gillius.jfxutils.chart.jfxchartutil; import org.gillius.jfxutils.chart.stableticksaxis; import java.text.simpledateformat; import java.util.timezone; public class test extends application { public static void main( string[] args ) { launch( args ); } @fxml private linechart<number, number> chart; @fxml private slider valueslider; @fxml private label outputlabel; private xychart.series<number, number> series; private long starttime; private timeline adddatatimeline; @fxml void addsample() { series.getdata().add(new xychart.data(1, 23)); series.getdata().add(new xychart.data(2, 14)); series.getdata().add(new xychart.data(3, 15)); series.getdata().add(new xychart.data(4, 24)); series.getdata().add(new xychart.data(5, 34)); series.getdata().add(new xychart.data(6, 36)); series.getdata().add(new xychart.data(7, 22)); series.getdata().add(new xychart.data(8, 45)); series.getdata().add(new xychart.data(9, 43)); series.getdata().add(new xychart.data(10, 17)); series.getdata().add(new xychart.data(11, 29)); series.getdata().add(new xychart.data(12, 25)); } @fxml void autozoom() { chart.getxaxis().setautoranging( true ); chart.getyaxis().setautoranging( true ); } @fxml void toggleadd() { switch ( adddatatimeline.getstatus() ) { case paused: case stopped: adddatatimeline.play(); chart.getxaxis().setautoranging( true ); chart.getyaxis().setautoranging( true ); //animation looks horrible if we're updating lot chart.setanimated( false ); chart.getxaxis().setanimated( false ); chart.getyaxis().setanimated( false ); break; case running: adddatatimeline.stop(); //return animation since we're not updating lot chart.setanimated( true ); chart.getxaxis().setanimated( true ); chart.getyaxis().setanimated( true ); break; default: throw new assertionerror( "unknown status" ); } } @override public void start( stage stage ) throws exception { fxmlloader loader = new fxmlloader( getclass().getresource( "charting.fxml" ) ); region contentrootregion = (region) loader.load(); stackpane root = jfxutil.createscalepane( contentrootregion, 960, 540, false ); scene scene = new scene( root, root.getprefwidth(), root.getprefheight() ); stage.setscene( scene ); stage.settitle( "charting example" ); stage.show(); } @fxml void initialize() { starttime = system.currenttimemillis(); //set chart format dates on x axis // simpledateformat format = new simpledateformat( "hh:mm:ss" ); // format.settimezone( timezone.gettimezone( "gmt" ) ); // ((stableticksaxis) chart.getxaxis()).setaxistickformatter( // new fixedformattickformatter( format ) ); series = new xychart.series<number, number>(); series.setname( "data" ); chart.getdata().add( series ); adddatatimeline = new timeline( new keyframe( duration.millis( 250 ), new eventhandler<actionevent>() { @override public void handle( actionevent actionevent ) { addsample(); } } )); adddatatimeline.setcyclecount( animation.indefinite ); chart.setonmousemoved( new eventhandler<mouseevent>() { @override public void handle( mouseevent mouseevent ) { double xstart = chart.getxaxis().getlocaltoparenttransform().gettx(); double axisxrelativemouseposition = mouseevent.getx() - xstart; } } ); //panning works via either secondary (right) mouse or primary ctrl held down chartpanmanager panner = new chartpanmanager( chart ); panner.setmousefilter( new eventhandler<mouseevent>() { @override public void handle( mouseevent mouseevent ) { if ( mouseevent.getbutton() == mousebutton.secondary || ( mouseevent.getbutton() == mousebutton.primary && mouseevent.isshortcutdown() ) ) { //let through } else { mouseevent.consume(); } } } ); panner.start(); //zooming works via primary mouse button without ctrl held down jfxchartutil.setupzooming( chart, new eventhandler<mouseevent>() { @override public void handle( mouseevent mouseevent ) { if ( mouseevent.getbutton() != mousebutton.primary || mouseevent.isshortcutdown() ) mouseevent.consume(); } } ); jfxchartutil.adddoubleprimaryclickautorangehandler( chart ); } }
this code line chart. working until try insert string on xaxis. want have chart strings on xaxis. got error: javafx.scene.chart.categoryaxis cannot cast javafx.scene.chart.valueaxis
the problem have chart defined linechart<number, number>
.
to have chart string
x-values , number
y-values should set generic parameters as:
linechart<string, number> chart;
and use categoryaxis:
a axis implementation works on string categories each value unique category(tick mark) along axis.
note: chart coming fxml file, have update fxml file also.
an example:
categoryaxis xaxis = new categoryaxis(); numberaxis yaxis = new numberaxis(); linechart<string,number> chart = new linechart<string,number>(xaxis,yaxis);
furthermore, have update declaration , initialization of series also:
private xychart.series<string, number> series; ... series = new xychart.series<string, number>();
then can add data series as:
series.getdata().add(new xychart.data("somestring", 23));
you can check creating categories line chart section (example 3-4) of original tutorial.
update:
it found out jfxutils package used not supporting zomming in case of valueaxis
. possible solution create zooming bar charts suggested in this answer.
Comments
Post a Comment