unit GraphAnnualTemps;

  interface
    uses
      AdsData      (* used by AdsFunc *),
      AdsFunc      (* used by AdsTable *),
      AdsTable     (* tAdsQuery *),
      ArrowCha     (* tArrowSeries *),
      Chart        (* used by DbChart *),
      Classes      (**),
      Controls     (* tDate *),
      DB           (* used by Series*),
      DbChart      (* tDBChart *),
      ExtCtrls     (* used by DbChart *),
      Forms        (* tForm *),
      Menus        (* tMainMenu, tMenuItem *),
      Series       (* tLineSeries *),
      ScaleManager (* tCelsiusScale *),
      TeEngine     (* tChartAxis; also used by DbChart *),
      TeeProcs     (* used by DbChart *),
      StdCtrls     (* tLabel*),
      Windows      (**);

    type
      tfmAnnualTemps = class(tForm)
          qLatestSummary: tAdsQuery;
          dbchartAnnualTemps: tDBChart;
            qAveragePriorYears:  tAdsQuery;
              serOldAverage: tLineSeries;
            qRang2016ToLastYear: tAdsQuery;
              ser2016toLastYear: tArrowSeries;
            qRangeTo2015:        tAdsQuery;
              serRangeTo2015: tArrowSeries;
            qThisYear:           tAdsQuery;
              serThisYear: tArrowSeries;
          menuMain: tMainMenu;
            menuGrid:  tMenuItem;
            menuPrint: tMenuItem;
          procedure FormDestroy   (Sender: tObject);
          procedure FormShow      (Sender: tObject);
          procedure menuGridClick (Sender: tObject);
          procedure menuPrintClick(Sender: tObject);
        private
          CelsiusScale: tCelsiusScale;
        public
          procedure Initialize;
        end (* tfmAnnualTemps *);

    var
      fmAnnualTemps: tfmAnnualTemps;

  implementation
    uses
      DateUtils    (* Today, YearOf *),
      Graphics     (* clBlue, fsBold *),
      HC3Utilities (* FtoC *),
      SysUtils     (* Format, FormatDateTime *);
      {$R *.dfm}

    procedure tfmAnnualTemps.FormDestroy(Sender: tObject);
      (* reclaim storage *)
    
      begin (* tfmAnnualTemps.FormDestroy *)
        if assigned(CelsiusScale) then FreeAndNil(CelsiusScale)
      end   (* tfmAnnualTemps.FormDestroy *);

    procedure tfmAnnualTemps.FormShow(Sender: tObject);
      (* initialize *)

      const
        InnerAxisPosition = -1;
        VOutmostMargin = -50;

      var
        LatestSummary: tDate;
        LeftEdge, ThisYear: integer;

      begin (* tfmAnnualTemps.FormShow *)
        with qLatestSummary do
          begin (* qLatestSummary *)
            Close;
            Open;
            LatestSummary := Fields[0].AsDateTime;
            Close
          end   (* qLatestSummary *);
        ThisYear := YearOf(Today());

        dbchartAnnualTemps.Title.Text.Text :=
          FormatDateTime('"temperature ranges, 2009-09-12 to "yyyy-mm-dd',
                         LatestSummary);

        qRangeTo2015.Close;
        qRangeTo2015.Open;

        ser2016ToLastYear.Legend.Text := Format('range, 2016 to %d', [ThisYear-1]);
        qRang2016ToLastYear.Close;
        qRang2016ToLastYear.Open;

        serThisYear.Legend.Text := Format('range, %d', [ThisYear]);
        qThisYear.Close;
        qThisYear.Open;

        serOldAverage.Legend.Text := Format('average, 2009 to %d', [ThisYear-1]);
        qAveragePriorYears.Close;
        qAveragePriorYears.Open;

        with CelsiusScale, dbchartAnnualTemps do
          begin (* CelsiusScale, dbchartAnnualTemps *)
            if Inner then
                LeftEdge := ShowAxes(CelsiusScale, LeftAxis, InnerAxisPosition)
              else
                LeftEdge := ShowAxes(LeftAxis, CelsiusScale, InnerAxisPosition);
            MarginLeft := -LeftEdge + VOutmostMargin
          end   (* CelsiusScale, dbchartAnnualTemps *)
      end   (* tfmAnnualTemps.FormShow *);

    procedure tfmAnnualTemps.Initialize;
      (* copied from GenericGraph *)

      const
        TooManyGridLines = 20;

      begin (* tfmAnnualTemps.Initialize *)
        if assigned(CelsiusScale) then
          CelsiusScale.Free;
        CelsiusScale := tCelsiusScale.Create(dbChartAnnualTemps.LeftAxis);
        CelsiusScale.AdjustLimits(TooManyGridLines)
      end   (* tfmAnnualTemps.Initialize *);

    procedure tfmAnnualTemps.menuGridClick(Sender: tObject);
      (* toggle °F/°C *)

      begin (* tfmAnnualTemps.menuGridClick *)
        menuGrid.Caption := CelsiusScale.ToggleGrid
      end   (* tfmAnnualTemps.menuGridClick *);

    procedure tfmAnnualTemps.menuPrintClick(Sender: tObject);
      (* use default printer *)

      begin (* tfmAnnualTemps.menuPrintClick *)
        dbchartAnnualTemps.PrintLandscape
      end   (* tfmAnnualTemps.menuPrintClick *);

end.
