comparison artifacts/src/main/java/org/dive4elements/river/exports/fixings/FixWQCurveGenerator.java @ 9123:1cc7653ca84f

Cleanup of ChartGenerator and ChartGenerator2 code. Put some of the copy/pasted code into a common abstraction.
author gernotbelger
date Tue, 05 Jun 2018 19:21:16 +0200
parents 5e38e2924c07
children 094ed9d1f2ad
comparison
equal deleted inserted replaced
9122:b8e7f6becf78 9123:1cc7653ca84f
26 import org.jfree.ui.RectangleInsets; 26 import org.jfree.ui.RectangleInsets;
27 import org.jfree.ui.TextAnchor; 27 import org.jfree.ui.TextAnchor;
28 28
29 import org.dive4elements.artifactdatabase.state.ArtifactAndFacet; 29 import org.dive4elements.artifactdatabase.state.ArtifactAndFacet;
30 import org.dive4elements.artifactdatabase.state.Facet; 30 import org.dive4elements.artifactdatabase.state.Facet;
31 import org.dive4elements.artifacts.CallContext;
31 import org.dive4elements.river.artifacts.D4EArtifact; 32 import org.dive4elements.river.artifacts.D4EArtifact;
32 import org.dive4elements.river.artifacts.StaticWKmsArtifact; 33 import org.dive4elements.river.artifacts.StaticWKmsArtifact;
33 import org.dive4elements.river.artifacts.WINFOArtifact; 34 import org.dive4elements.river.artifacts.WINFOArtifact;
34 import org.dive4elements.river.artifacts.access.FixAnalysisAccess; 35 import org.dive4elements.river.artifacts.access.FixAnalysisAccess;
35 import org.dive4elements.river.artifacts.model.DateRange; 36 import org.dive4elements.river.artifacts.model.DateRange;
113 /** Needed to access data to create subtitle. */ 114 /** Needed to access data to create subtitle. */
114 protected D4EArtifact artifact; 115 protected D4EArtifact artifact;
115 116
116 /** Returns value != 0 if the current km is not at a gauge. */ 117 /** Returns value != 0 if the current km is not at a gauge. */
117 public double getCurrentGaugeDatum() { 118 public double getCurrentGaugeDatum() {
118 Object ckm = context.getContextValue(CURRENT_KM); 119 Object ckm = getContext().getContextValue(CURRENT_KM);
119 if (ckm != null) { 120 if (ckm != null) {
120 return DischargeCurveGenerator.getCurrentGaugeDatum( 121 return DischargeCurveGenerator.getCurrentGaugeDatum(
121 (Double) ckm, 122 (Double) ckm,
122 (D4EArtifact) getMaster(), 1e-4); 123 (D4EArtifact) getMaster(), 1e-4);
123 } 124 }
208 ) { 209 ) {
209 String name = aaf.getFacetName(); 210 String name = aaf.getFacetName();
210 211
211 this.artifact = (D4EArtifact) aaf.getArtifact(); 212 this.artifact = (D4EArtifact) aaf.getArtifact();
212 213
214 final CallContext context = getContext();
215
213 if(name.startsWith(FIX_SECTOR_AVERAGE_WQ)) { 216 if(name.startsWith(FIX_SECTOR_AVERAGE_WQ)) {
214 doSectorAverageOut(aaf, doc, visible); 217 doSectorAverageOut(aaf, doc, visible);
215 } 218 }
216 else if(FIX_ANALYSIS_EVENTS_WQ.equals(name)) { 219 else if(FIX_ANALYSIS_EVENTS_WQ.equals(name)) {
217 doAnalysisEventsOut(aaf, doc, visible); 220 doAnalysisEventsOut(aaf, doc, visible);
284 ThemeDocument doc, 287 ThemeDocument doc,
285 boolean visible 288 boolean visible
286 ) { 289 ) {
287 log.debug("doSectorAverageOut"); 290 log.debug("doSectorAverageOut");
288 291
289 QWDDateRange qwdd = (QWDDateRange) aaf.getData(context); 292 QWDDateRange qwdd = (QWDDateRange) aaf.getData(getContext());
290 QWD qwd = qwdd != null ? qwdd.getQWD() : null; 293 QWD qwd = qwdd != null ? qwdd.getQWD() : null;
291 294
292 if(qwd != null) { 295 if(qwd != null) {
293 addQWSeries(new QWD[] { qwd }, aaf, doc, visible); 296 addQWSeries(new QWD[] { qwd }, aaf, doc, visible);
294 } 297 }
303 ThemeDocument doc, 306 ThemeDocument doc,
304 boolean visible 307 boolean visible
305 ) { 308 ) {
306 log.debug("doAnalysisEventsOut"); 309 log.debug("doAnalysisEventsOut");
307 310
308 QWD qwd = (QWD)aaf.getData(context); 311 QWD qwd = (QWD)aaf.getData(getContext());
309 312
310 if (qwd == null) { 313 if (qwd == null) {
311 log.debug("doAnalysisEventsOut: qwd == null"); 314 log.debug("doAnalysisEventsOut: qwd == null");
312 return; 315 return;
313 } 316 }
363 ThemeDocument doc, 366 ThemeDocument doc,
364 boolean visible 367 boolean visible
365 ) { 368 ) {
366 log.debug("doReferenceEventsOut"); 369 log.debug("doReferenceEventsOut");
367 370
368 QWI qwd = (QWI)aaf.getData(context); 371 QWI qwd = (QWI)aaf.getData(getContext());
369 if (qwd == null) { 372 if (qwd == null) {
370 log.debug("doReferenceEventsOut: qwds == null"); 373 log.debug("doReferenceEventsOut: qwds == null");
371 return; 374 return;
372 } 375 }
373 376
419 String title, 422 String title,
420 ThemeDocument theme, 423 ThemeDocument theme,
421 boolean visible 424 boolean visible
422 ) { 425 ) {
423 XYSeries series = new StyledXYSeries(title, theme); 426 XYSeries series = new StyledXYSeries(title, theme);
424 Double ckm = (Double) context.getContextValue(CURRENT_KM); 427 Double ckm = (Double) getContext().getContextValue(CURRENT_KM);
425 if (wqkms == null || wqkms.getKms().length == 0 || ckm == null) { 428 if (wqkms == null || wqkms.getKms().length == 0 || ckm == null) {
426 log.info("addPointFromWQKms: No event data to show."); 429 log.info("addPointFromWQKms: No event data to show.");
427 return; 430 return;
428 } 431 }
429 double[] kms = wqkms.getKms(); 432 double[] kms = wqkms.getKms();
456 ThemeDocument doc, 459 ThemeDocument doc,
457 boolean visible 460 boolean visible
458 ) { 461 ) {
459 log.debug("doEventsOut"); 462 log.debug("doEventsOut");
460 // Find W/Q at km. 463 // Find W/Q at km.
461 addPointFromWQKms((WQKms) aaf.getData(context), 464 addPointFromWQKms((WQKms) aaf.getData(getContext()),
462 aaf.getFacetDescription(), doc, visible); 465 aaf.getFacetDescription(), doc, visible);
463 } 466 }
464 467
465 468
466 protected void doWQCurveOut( 469 protected void doWQCurveOut(
470 ) { 473 ) {
471 log.debug("doWQCurveOut"); 474 log.debug("doWQCurveOut");
472 475
473 FixWQCurveFacet facet = (FixWQCurveFacet)aaf.getFacet(); 476 FixWQCurveFacet facet = (FixWQCurveFacet)aaf.getFacet();
474 FixFunction func = (FixFunction)facet.getData( 477 FixFunction func = (FixFunction)facet.getData(
475 aaf.getArtifact(), context); 478 aaf.getArtifact(), getContext());
476 479
477 if (func == null) { 480 if (func == null) {
478 log.warn("doWQCurveOut: Facet does not contain FixFunction"); 481 log.warn("doWQCurveOut: Facet does not contain FixFunction");
479 return; 482 return;
480 } 483 }
525 ThemeDocument doc, 528 ThemeDocument doc,
526 boolean visible 529 boolean visible
527 ) { 530 ) {
528 log.debug("doOutlierOut"); 531 log.debug("doOutlierOut");
529 532
530 QWI[] qws = (QWI[])aaf.getData(context); 533 QWI[] qws = (QWI[])aaf.getData(getContext());
531 addQWSeries(qws, aaf, doc, visible); 534 addQWSeries(qws, aaf, doc, visible);
532 } 535 }
533 536
534 537
535 /** Add markers for q sectors. */ 538 /** Add markers for q sectors. */
541 log.debug("doQSectorOut"); 544 log.debug("doQSectorOut");
542 if (!visible) { 545 if (!visible) {
543 return; 546 return;
544 } 547 }
545 548
546 Object qsectorsObj = aaf.getData(context); 549 Object qsectorsObj = aaf.getData(getContext());
547 if (qsectorsObj == null || !(qsectorsObj instanceof List)) { 550 if (qsectorsObj == null || !(qsectorsObj instanceof List)) {
548 log.warn("No QSectors coming from data."); 551 log.warn("No QSectors coming from data.");
549 return; 552 return;
550 } 553 }
551 554
629 else { 632 else {
630 // Assume its WKms. 633 // Assume its WKms.
631 log.debug("Got WKms"); 634 log.debug("Got WKms");
632 WKms data = (WKms) wqkms; 635 WKms data = (WKms) wqkms;
633 636
634 Double ckm = (Double) context.getContextValue(CURRENT_KM); 637 Double ckm = (Double) getContext().getContextValue(CURRENT_KM);
635 double location = (ckm != null) 638 double location = (ckm != null)
636 ? ckm.doubleValue() 639 ? ckm.doubleValue()
637 : getRange()[0]; 640 : getRange()[0];
638 double w = StaticWKmsArtifact.getWAtKmLin(data, location); 641 double w = StaticWKmsArtifact.getWAtKmLin(data, location);
639 xy.add(new StickyAxisAnnotation(aandf.getFacetDescription(), 642 xy.add(new StickyAxisAnnotation(aandf.getFacetDescription(),
707 if (wqkms instanceof WQKms) { 710 if (wqkms instanceof WQKms) {
708 // TODO As in doEventsOut, the value-searching should 711 // TODO As in doEventsOut, the value-searching should
709 // be delivered by the facet already (instead of in the Generator). 712 // be delivered by the facet already (instead of in the Generator).
710 log.debug("FixWQCurveGenerator: doWQOut: WQKms"); 713 log.debug("FixWQCurveGenerator: doWQOut: WQKms");
711 714
712 addPointFromWQKms((WQKms)aaf.getData(context), 715 addPointFromWQKms((WQKms)aaf.getData(getContext()),
713 aaf.getFacetDescription(), theme, visible); 716 aaf.getFacetDescription(), theme, visible);
714 } 717 }
715 else { 718 else {
716 log.debug("FixWQCurveGenerator: doWQOut: double[][]"); 719 log.debug("FixWQCurveGenerator: doWQOut: double[][]");
717 double [][] data = (double [][]) wqkms; 720 double [][] data = (double [][]) wqkms;
771 addAnnotations(flysAnno); 774 addAnnotations(flysAnno);
772 } 775 }
773 } 776 }
774 777
775 @Override 778 @Override
776 protected String getChartTitle() { 779 protected String getChartTitle(final CallContext context) {
777 return Resources.format( 780 return Resources.format(
778 context.getMeta(), 781 context.getMeta(),
779 I18N_CHART_TITLE, 782 I18N_CHART_TITLE,
780 I18N_CHART_TITLE_DEFAULT, 783 I18N_CHART_TITLE_DEFAULT,
781 context.getContextValue(CURRENT_KM)); 784 context.getContextValue(CURRENT_KM));
782 } 785 }
783 786
784 @Override 787 @Override
785 protected String getDefaultChartTitle() { 788 protected String getDefaultChartTitle(final CallContext context) {
786 return msg(I18N_CHART_TITLE, I18N_CHART_TITLE_DEFAULT); 789 return msg(I18N_CHART_TITLE, I18N_CHART_TITLE_DEFAULT);
787 } 790 }
788 791
789 @Override 792 @Override
790 protected String getDefaultChartSubtitle() { 793 protected String getDefaultChartSubtitle(final CallContext context) {
791 FixAnalysisAccess access = new FixAnalysisAccess(artifact); 794 FixAnalysisAccess access = new FixAnalysisAccess(artifact);
792 DateRange dateRange = access.getDateRange(); 795 DateRange dateRange = access.getDateRange();
793 DateRange refRange = access.getReferencePeriod(); 796 DateRange refRange = access.getReferencePeriod();
794 797
795 if (dateRange != null && refRange != null) { 798 if (dateRange != null && refRange != null) {
806 809
807 return null; 810 return null;
808 } 811 }
809 812
810 @Override 813 @Override
811 protected void addSubtitles(JFreeChart chart) { 814 protected void addSubtitles(final CallContext context, final JFreeChart chart) {
812 String defaultSubtitle = getDefaultChartSubtitle(); 815 String defaultSubtitle = getDefaultChartSubtitle(getContext());
813 816
814 if (defaultSubtitle == null || defaultSubtitle.length() == 0) { 817 if (defaultSubtitle == null || defaultSubtitle.length() == 0) {
815 return; 818 return;
816 } 819 }
817 820
839 842
840 chart.addSubtitle(new TextTitle(buf.toString())); 843 chart.addSubtitle(new TextTitle(buf.toString()));
841 } 844 }
842 845
843 @Override 846 @Override
844 protected String getDefaultXAxisLabel() { 847 protected String getDefaultXAxisLabel(final CallContext context) {
845 return msg(I18N_XAXIS_LABEL, I18N_XAXIS_LABEL_DEFAULT); 848 return msg(I18N_XAXIS_LABEL, I18N_XAXIS_LABEL_DEFAULT);
846 } 849 }
847 850
848 @Override 851 @Override
849 protected String getDefaultYAxisLabel(int pos) { 852 protected String getDefaultYAxisLabel(int pos) {
850 D4EArtifact flys = (D4EArtifact) master; 853 D4EArtifact flys = getArtifact();
851 854
852 String unit = pos == 0 855 String unit = pos == 0
853 ? "cm" 856 ? "cm"
854 : RiverUtils.getRiver(flys).getWstUnit().getName(); 857 : RiverUtils.getRiver(flys).getWstUnit().getName();
855 858

http://dive4elements.wald.intevation.org