// $Revision: 1.16 $ // ViewerDrawers.C // Copyright (C) 1994, 1995 Taligent, Inc. All rights reserved. #ifndef TaligentSamples_VIEWERDRAWERS #include "ViewerDrawers.h" #endif #ifndef TaligentSamples_STANDARDGRAPH #include #endif #ifndef Taligent_ASSERTIONS #include #endif //============================================================================== // TPriceDrawer TaligentTypeExtensionMacro(TPriceDrawer); TPriceDrawer::TPriceDrawer() : TGraphDrawer(), fStocks(NIL), fDataPointWidth(0), fDataPointInterval(0), fLine(), fClosePriceBox() { } TPriceDrawer::TPriceDrawer(GraphValue dataPointWidth, GraphValue dataPointInterval, TCollectionOf* stocksForAdoption) : TGraphDrawer(), fStocks(stocksForAdoption), fDataPointWidth(dataPointWidth), fDataPointInterval(dataPointInterval), fLine(), fClosePriceBox() { static const TColor& kDefaultLineColor = *TColorPaint::GetBlack().GetColor(); static const TColor& kDefaultBoxColor = *TColorPaint::GetRed().GetColor(); const TGPoint kDefaultBoxSize(3.0, 3.0); fLine = TLine(TGLine(), new TGrafBundle(kDefaultLineColor)); fClosePriceBox = TArea(TGRect(TGPoint::kOrigin, kDefaultBoxSize), new TGrafBundle(kDefaultBoxColor, TGrafBundle::kFill)); } TPriceDrawer::TPriceDrawer(const TPriceDrawer& source) : TGraphDrawer(source), fStocks(NIL), fDataPointWidth(source.fDataPointWidth), fDataPointInterval(source.fDataPointInterval), fLine(source.fLine), fClosePriceBox(source.fClosePriceBox) { if (source.fStocks != NIL) { CopyStocks(*source.fStocks); } } TPriceDrawer::~TPriceDrawer() { DeleteStocks(); } TPriceDrawer& TPriceDrawer::operator=(const TPriceDrawer& source) { if (&source != this) { TGraphDrawer::operator=(source); if (source.fStocks == NIL) { DeleteStocks(); } else { CopyStocks(*source.fStocks); } fDataPointWidth = source.fDataPointWidth; fDataPointInterval = source.fDataPointInterval; fLine = source.fLine; fClosePriceBox = source.fClosePriceBox; } return *this; } TStream& TPriceDrawer::operator>>=(TStream& toStream) const { ::WriteVersion(toStream, kOriginalVersion); TGraphDrawer::operator>>=(toStream); ::Flatten(fStocks, toStream); fDataPointWidth >>= toStream; fDataPointInterval >>= toStream; fLine >>= toStream; fClosePriceBox >>= toStream; return toStream; } TStream& TPriceDrawer::operator<<=(TStream& fromStream) { ::ReadVersion(fromStream, kOriginalVersion, kOriginalVersion); TGraphDrawer::operator<<=(fromStream); DeleteStocks(); ::Resurrect(fStocks, fromStream); fDataPointWidth <<= fromStream; fDataPointInterval <<= fromStream; fLine <<= fromStream; fClosePriceBox <<= fromStream; return fromStream; } GraphValue TPriceDrawer::GetDataPointWidth() const { return fDataPointWidth; } GraphValue TPriceDrawer::GetDataPointInterval() const { return fDataPointInterval; } void TPriceDrawer::SetDataPointWidth(GraphValue width) { fDataPointWidth = width; } void TPriceDrawer::SetDataPointInterval(GraphValue interval) { fDataPointInterval = interval; } void TPriceDrawer::SetLineColor(const TColor& color) { delete fLine.OrphanBundle(); fLine.AdoptBundle(new TGrafBundle(color)); } void TPriceDrawer::SetBoxColor(const TColor& color) { delete fClosePriceBox.OrphanBundle(); fClosePriceBox.AdoptBundle(new TGrafBundle(color, TGrafBundle::kFill)); } void TPriceDrawer::SetBoxSize(const TGPoint& size) { TGrafBundle* bundle = fClosePriceBox.OrphanBundle(); fClosePriceBox = TArea(TGRect(TGPoint::kOrigin, size)); fClosePriceBox.AdoptBundle(bundle); } void TPriceDrawer::DrawIntoGraph(TGrafPort& port) const { if (fStocks != NIL) { const TStandardGraph& graph = GetTargetGraph(); TLine line(fLine); TArea closePriceBox(fClosePriceBox); TGrafMatrix matrix; GraphValue xValue = graph.GetXRange().GetMin() + GetDataPointInterval(); TGPoint startPoint; TDeleterFor > iterator = fStocks->CreateIterator(); for (TStockDay* stockDay = iterator->First(); stockDay != NIL; stockDay = iterator->Next()) { if (!stockDay->IsHoliday()) { // Draw vertical line. startPoint = graph.MapValuesToPoint(xValue, stockDay->GetHigh()); line.SetStartPoint(startPoint); line.SetEndPoint(graph.MapValuesToPoint(xValue, stockDay->GetLow())); line.Draw(port); // Draw high-price line. line.SetStartPoint(TGPoint(startPoint.fX - (GetDataPointWidth() - 1) / 2, startPoint.fY)); line.SetEndPoint(TGPoint(startPoint.fX + (GetDataPointWidth() - 1) / 2, startPoint.fY)); line.Draw(port); // Draw low-price line. startPoint = graph.MapValuesToPoint(xValue, stockDay->GetLow()); line.SetStartPoint(TGPoint(startPoint.fX - (GetDataPointWidth() - 1) / 2, startPoint.fY)); line.SetEndPoint(TGPoint(startPoint.fX + (GetDataPointWidth() - 1) / 2, startPoint.fY)); line.Draw(port); // Draw the close-price box. startPoint = graph.MapValuesToPoint(xValue, stockDay->GetClose()); matrix.SetToTranslate(TGPoint(long(startPoint.fX - 1), startPoint.fY - 1)); closePriceBox.TransformBy(matrix); closePriceBox.Draw(port); closePriceBox = fClosePriceBox; } xValue += GetDataPointInterval(); } } } TGRect TPriceDrawer::GetBounds() const { TGRect totalBounds(TGRect::kZeroRect); if (fStocks != NIL) { TGPoint point; const TStandardGraph& graph = GetTargetGraph(); GraphValue xValue = GetDataPointInterval(); TDeleterFor > iterator = fStocks->CreateIterator(); for (TStockDay* stockDay = iterator->First(); stockDay != NIL; stockDay = iterator->Next()) { if (!stockDay->IsHoliday()) { point = graph.MapValuesToPoint(xValue, stockDay->GetHigh()); point.fX -= GetDataPointWidth() / (GraphValue)2; totalBounds.ExtendTo(point); point = graph.MapValuesToPoint(xValue, stockDay->GetLow()); point.fX += GCoordinate(GetDataPointWidth() / (GraphValue)2); totalBounds.ExtendTo(point); } xValue += GetDataPointInterval(); } } return totalBounds; } void TPriceDrawer::CopyStocks(const TCollectionOf& source) { DeleteStocks(); fStocks = ::Copy(source); fStocks->RemoveAll(); TDeleterFor > iterator = source.CreateIterator(); for (TStockDay* stockDay = iterator->First(); stockDay != NIL; stockDay = iterator->Next()) { fStocks->Add(new TStockDay(*stockDay)); } } void TPriceDrawer::DeleteStocks() { if (fStocks != NIL) { fStocks->DeleteAll(); delete fStocks; fStocks = NIL; } } //============================================================================== // TVolumeDrawer TaligentTypeExtensionMacro(TVolumeDrawer); TVolumeDrawer::TVolumeDrawer() : TBarDrawer() { } TVolumeDrawer::TVolumeDrawer( GraphValue xAxisLocation, GraphValue barWidth, GraphValue barInterval, const TCollectionOf& stocks) : TBarDrawer(xAxisLocation, barWidth, barInterval, new TRawArray) { SetVolumeInformation(stocks); } TVolumeDrawer::TVolumeDrawer(const TVolumeDrawer& source) : TBarDrawer(source) { } TVolumeDrawer::~TVolumeDrawer() { } TVolumeDrawer& TVolumeDrawer::operator=(const TVolumeDrawer& source) { if (&source != this) { TBarDrawer::operator=(source); } return *this; } TStream& TVolumeDrawer::operator>>=(TStream& toStream) const { ::WriteVersion(toStream, kOriginalVersion); TBarDrawer::operator>>=(toStream); return toStream; } TStream& TVolumeDrawer::operator<<=(TStream& fromStream) { ::ReadVersion(fromStream, kOriginalVersion, kOriginalVersion); TBarDrawer::operator<<=(fromStream); return fromStream; } void TVolumeDrawer::SetVolumeInformation(const TCollectionOf& stocks) { TDeleterFor > iterator = stocks.CreateIterator(); TRawArray* array = new TRawArray(stocks.Count()); TStockDay* stockDay = NIL; unsigned long index = 0; for (stockDay = iterator->First(), index = 0; stockDay != NIL; stockDay = iterator->Next(), ++index) { (*array)[index] = stockDay->GetVolume(); } AdoptValues(array); }