Mercurial > dive4elements > river
comparison flys-artifacts/src/main/java/de/intevation/flys/artifacts/charts/CrossSectionApp.java @ 1858:94871b7ce9e9
CrossSectionApp: split curves in separated datasets to make them renderable with different renderers.
flys-artifacts/trunk@3207 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author | Sascha L. Teichmann <sascha.teichmann@intevation.de> |
---|---|
date | Thu, 10 Nov 2011 09:37:59 +0000 |
parents | d562772a418e |
children | 0e9c0b49b4ef |
comparison
equal
deleted
inserted
replaced
1857:7fc87a67e6f7 | 1858:94871b7ce9e9 |
---|---|
59 | 59 |
60 import org.jfree.chart.plot.PlotOrientation; | 60 import org.jfree.chart.plot.PlotOrientation; |
61 import org.jfree.chart.plot.XYPlot; | 61 import org.jfree.chart.plot.XYPlot; |
62 | 62 |
63 import org.jfree.data.xy.XYSeries; | 63 import org.jfree.data.xy.XYSeries; |
64 import org.jfree.data.xy.XYDataset; | |
64 import org.jfree.data.xy.XYSeriesCollection; | 65 import org.jfree.data.xy.XYSeriesCollection; |
66 import org.jfree.chart.renderer.xy.XYItemRenderer; | |
65 | 67 |
66 import org.jfree.ui.ApplicationFrame; | 68 import org.jfree.ui.ApplicationFrame; |
67 import org.jfree.ui.RefineryUtilities; | 69 import org.jfree.ui.RefineryUtilities; |
68 | 70 |
69 public class CrossSectionApp | 71 public class CrossSectionApp |
332 protected void updateChart() { | 334 protected void updateChart() { |
333 | 335 |
334 CrossSectionLineItem csli = | 336 CrossSectionLineItem csli = |
335 (CrossSectionLineItem)crossSectionLinesCB.getSelectedItem(); | 337 (CrossSectionLineItem)crossSectionLinesCB.getSelectedItem(); |
336 | 338 |
337 JFreeChart chart = createChart(csli == null | 339 JFreeChart chart = createChart(); |
338 ? new XYSeriesCollection() | |
339 : generateDataset()); | |
340 | 340 |
341 chartPanel.setChart(chart); | 341 chartPanel.setChart(chart); |
342 } | 342 } |
343 | 343 |
344 protected ChartPanel createChartPanel() { | 344 protected ChartPanel createChartPanel() { |
345 CrossSectionLineItem csli = | 345 CrossSectionLineItem csli = |
346 (CrossSectionLineItem)crossSectionLinesCB.getSelectedItem(); | 346 (CrossSectionLineItem)crossSectionLinesCB.getSelectedItem(); |
347 | 347 |
348 JFreeChart chart = createChart(csli == null | 348 JFreeChart chart = createChart(); |
349 ? new XYSeriesCollection() | |
350 : generateDataset()); | |
351 | 349 |
352 return new ChartPanel(chart); | 350 return new ChartPanel(chart); |
353 } | 351 } |
354 | 352 |
355 protected void dumpData() { | 353 protected void dumpData() { |
402 out.close(); | 400 out.close(); |
403 } | 401 } |
404 } | 402 } |
405 } | 403 } |
406 | 404 |
407 public void generateWaterLevels( | 405 public XYDataset generateWaterLevels( |
406 List<Point2D> points | |
407 ) { | |
408 if (points == null || points.isEmpty() || lastWaterLevel == null) { | |
409 return null; | |
410 } | |
411 | |
412 double [][] data = Lines.createWaterLines(points, lastWaterLevel); | |
413 XYSeries series = | |
414 new XYSeries(String.valueOf(lastWaterLevel), false); | |
415 | |
416 double [] x = data[0]; | |
417 double [] y = data[1]; | |
418 for (int i = 0; i < x.length; ++i) { | |
419 series.add(x[i], y[i], false); | |
420 } | |
421 | |
422 return new XYSeriesCollection(series); | |
423 } | |
424 | |
425 public XYDataset generateProfile( | |
408 List<Point2D> points, | 426 List<Point2D> points, |
409 XYSeriesCollection collection | 427 String legend |
410 ) { | 428 ) { |
411 if (points == null || points.isEmpty()) { | 429 if (points == null || points.isEmpty()) { |
412 return; | 430 return null; |
413 } | |
414 | |
415 if (lastWaterLevel != null) { | |
416 double [][] data = Lines.createWaterLines(points, lastWaterLevel); | |
417 XYSeries series = | |
418 new XYSeries(String.valueOf(lastWaterLevel), false); | |
419 | |
420 double [] x = data[0]; | |
421 double [] y = data[1]; | |
422 for (int i = 0; i < x.length; ++i) { | |
423 series.add(x[i], y[i], false); | |
424 } | |
425 | |
426 collection.addSeries(series); | |
427 } | |
428 } | |
429 | |
430 public void generateProfile( | |
431 List<Point2D> points, | |
432 String legend, | |
433 XYSeriesCollection collection | |
434 ) { | |
435 if (points == null || points.isEmpty()) { | |
436 return; | |
437 } | 431 } |
438 | 432 |
439 double [][] values = CrossSectionLine.fetchCrossSectionProfile(points); | 433 double [][] values = CrossSectionLine.fetchCrossSectionProfile(points); |
440 | 434 |
441 XYSeries series = new XYSeries(legend, false); | 435 XYSeries series = new XYSeries(legend, false); |
444 double [] y = values[1]; | 438 double [] y = values[1]; |
445 for (int i = 0; i < x.length; ++i) { | 439 for (int i = 0; i < x.length; ++i) { |
446 series.add(x[i], y[i], false); | 440 series.add(x[i], y[i], false); |
447 } | 441 } |
448 | 442 |
449 collection.addSeries(series); | 443 return new XYSeriesCollection(series); |
450 } | 444 } |
451 | 445 |
452 public XYSeriesCollection generateDataset() { | 446 public List<Pair<XYDataset, XYItemRenderer>> generateDatasets() { |
453 XYSeriesCollection collection = new XYSeriesCollection(); | 447 List<Pair<XYDataset, XYItemRenderer>> pairs = |
448 new ArrayList<Pair<XYDataset, XYItemRenderer>>(); | |
449 | |
450 return pairs; | |
451 } | |
452 | |
453 public List<Pair<XYDataset, XYItemRenderer>> generateDataset() { | |
454 | |
455 List<Pair<XYDataset, XYItemRenderer>> datasets = | |
456 new ArrayList<Pair<XYDataset, XYItemRenderer>>(); | |
454 | 457 |
455 CrossSectionLineItem csli = | 458 CrossSectionLineItem csli = |
456 (CrossSectionLineItem)crossSectionLinesCB.getSelectedItem(); | 459 (CrossSectionLineItem)crossSectionLinesCB.getSelectedItem(); |
457 | 460 |
458 for (int i = 0; i < drawCrossSection.length; ++i) { | 461 for (int i = 0; i < drawCrossSection.length; ++i) { |
461 if (drawCrossSection[i]) { | 464 if (drawCrossSection[i]) { |
462 CrossSection cs = crossSections.get(i); | 465 CrossSection cs = crossSections.get(i); |
463 for (Pair<CrossSection, CrossSectionLine> csl: csli.lines) { | 466 for (Pair<CrossSection, CrossSectionLine> csl: csli.lines) { |
464 if (csl.getA() == cs) { | 467 if (csl.getA() == cs) { |
465 points = csl.getB().fetchCrossSectionLinesPoints(); | 468 points = csl.getB().fetchCrossSectionLinesPoints(); |
466 generateProfile( | 469 |
467 points, | 470 XYDataset dataset = generateProfile( |
468 cs.getDescription(), | 471 points, cs.getDescription()); |
469 collection); | 472 if (dataset != null) { |
473 datasets.add(new Pair<XYDataset, XYItemRenderer>( | |
474 dataset, null)); | |
475 } | |
470 break; | 476 break; |
471 } | 477 } |
472 } | 478 } |
473 } | 479 } |
474 | 480 |
477 for (Pair<CrossSection, CrossSectionLine> csl: csli.lines) { | 483 for (Pair<CrossSection, CrossSectionLine> csl: csli.lines) { |
478 if (csl.getA() == cs) { | 484 if (csl.getA() == cs) { |
479 if (points != null) { | 485 if (points != null) { |
480 points = csl.getB().fetchCrossSectionLinesPoints(); | 486 points = csl.getB().fetchCrossSectionLinesPoints(); |
481 } | 487 } |
482 generateWaterLevels( | 488 XYDataset dataset = generateWaterLevels(points); |
483 points, | 489 if (dataset != null) { |
484 collection); | 490 datasets.add(new Pair<XYDataset, XYItemRenderer>( |
491 dataset, null)); | |
492 } | |
485 break; | 493 break; |
486 } | 494 } |
487 } | 495 } |
488 } | 496 } |
489 | 497 |
490 } | 498 } |
491 | 499 |
492 return collection; | 500 return datasets; |
493 } | 501 } |
494 | 502 |
495 protected Object [] createCrossSectionLineItems( | 503 protected Object [] createCrossSectionLineItems( |
496 Map<Double, List<Pair<CrossSection, CrossSectionLine>>> km2lines | 504 Map<Double, List<Pair<CrossSection, CrossSectionLine>>> km2lines |
497 ) { | 505 ) { |
505 } | 513 } |
506 return result; | 514 return result; |
507 } | 515 } |
508 | 516 |
509 | 517 |
510 public static JFreeChart createChart(XYSeriesCollection collection) { | 518 public JFreeChart createChart() { |
511 JFreeChart chart = ChartFactory.createXYLineChart( | 519 JFreeChart chart = ChartFactory.createXYLineChart( |
512 null, | 520 null, |
513 "Abstand [m]", | 521 "Abstand [m]", |
514 "H\u00f6he [m]", | 522 "H\u00f6he [m]", |
515 collection, | 523 null, |
516 PlotOrientation.VERTICAL, | 524 PlotOrientation.VERTICAL, |
517 true, | 525 true, |
518 true, | 526 true, |
519 false); | 527 false); |
520 | 528 |
529 List<Pair<XYDataset, XYItemRenderer>> datasets = | |
530 generateDataset(); | |
531 | |
521 XYPlot plot = chart.getXYPlot(); | 532 XYPlot plot = chart.getXYPlot(); |
533 | |
534 for (int i = 0, N = datasets.size(); i < N; ++i) { | |
535 Pair<XYDataset, XYItemRenderer> p = datasets.get(i); | |
536 plot.setDataset(i, p.getA()); | |
537 plot.mapDatasetToRangeAxis(i, 0); | |
538 XYItemRenderer renderer = p.getB(); | |
539 if (renderer != null) { | |
540 plot.setRenderer(i, renderer); | |
541 } | |
542 } | |
543 | |
522 NumberAxis yAxis = (NumberAxis)plot.getRangeAxis(); | 544 NumberAxis yAxis = (NumberAxis)plot.getRangeAxis(); |
523 yAxis.setAutoRangeIncludesZero(false); | 545 yAxis.setAutoRangeIncludesZero(false); |
524 | 546 |
525 ChartUtilities.applyCurrentTheme(chart); | 547 ChartUtilities.applyCurrentTheme(chart); |
526 return chart; | 548 return chart; |