// $Revision: 1.8 $ // BarDrawer.C // Copyright (C) 1994, 1995 Taligent, Inc. All rights reserved #ifndef TaligentSamples_BARDRAWER #include "BarDrawer.h" #endif #ifndef TaligentSamples_AXISDRAWER #include "AxisDrawer.h" #endif #ifndef TaligentSamples_STANDARDGRAPH #include "StandardGraph.h" #endif #ifndef Taligent_RAWARRAY #include #endif #ifndef Taligent_RGBCOLOR #include #endif //============================================================================== // TBarDrawer const TRGBColor kDefaultBarColor(0, 0, 0); TaligentTypeExtensionMacro(TBarDrawer); TBarDrawer::TBarDrawer() : TGraphDrawer(), fBarWidth(0.), fBarInterval(0.), fValues(), fBar(), fXAxisLocation(0) { fBar = TArea(TGRect(TGPoint::kOrigin, TGPoint(1, 1)), new TGrafBundle(kDefaultBarColor, TGrafBundle::kFill)); fValues = new TRawArray; } TBarDrawer::TBarDrawer( GraphValue xAxisLocation, GraphValue barWidth, GraphValue barInterval, TRawArray* valuesForAdoption) : TGraphDrawer(), fBarWidth(barWidth), fBarInterval(barInterval), fValues(valuesForAdoption), fBar(), fXAxisLocation(xAxisLocation) { fBar = TArea(TGRect(TGPoint::kOrigin, TGPoint(1, 1)), new TGrafBundle(kDefaultBarColor, TGrafBundle::kFill)); } TBarDrawer::TBarDrawer(const TBarDrawer& source) : TGraphDrawer(source), fBarWidth(source.fBarWidth), fBarInterval(source.fBarInterval), fValues(), fBar(source.fBar), fXAxisLocation(source.fXAxisLocation) { fValues = new TRawArray(*source.fValues); } TBarDrawer::~TBarDrawer() { delete fValues; } TBarDrawer& TBarDrawer::operator=(const TBarDrawer& source) { if (&source != this) { TGraphDrawer::operator=(source); fBarWidth = source.fBarWidth; fBarInterval = source.fBarInterval; *fValues = *source.fValues; fBar = source.fBar; fXAxisLocation = source.fXAxisLocation; } return *this; } TStream& TBarDrawer::operator>>=(TStream& toStream) const { ::WriteVersion(toStream, kOriginalVersion); TGraphDrawer::operator>>=(toStream); fBarWidth >>= toStream; fBarInterval >>= toStream; *fValues >>= toStream; fBar >>= toStream; fXAxisLocation >>= toStream; return toStream; } TStream& TBarDrawer::operator<<=(TStream& fromStream) { ::ReadVersion(fromStream, kOriginalVersion, kOriginalVersion); TGraphDrawer::operator<<=(fromStream); fBarWidth <<= fromStream; fBarInterval <<= fromStream; *fValues <<= fromStream; fBar <<= fromStream; fXAxisLocation <<= fromStream; return fromStream; } void TBarDrawer::AdoptValues(TRawArray* values) { delete fValues; fValues = values; } TRawArray* TBarDrawer::OrphanValues() { TRawArray* orphan = fValues; fValues = new TRawArray; return orphan; } GraphValue TBarDrawer::GetBarWidth() const { return fBarWidth; } GraphValue TBarDrawer::GetBarInterval() const { return fBarInterval; } void TBarDrawer::SetBarWidth(const GraphValue& width) { fBarWidth = width; } void TBarDrawer::SetBarInterval(const GraphValue& interval) { fBarInterval = interval; } void TBarDrawer::SetBarColor(const TRGBColor& color) { delete fBar.OrphanBundle(); fBar.AdoptBundle(new TGrafBundle(color, TGrafBundle::kFill)); } void TBarDrawer::DrawIntoGraph(TGrafPort& port) const { TArea bar = fBar; TGrafMatrix matrix; const TStandardGraph& graph = GetTargetGraph(); GraphValue xValue = graph.GetXRange().GetMin() + GetBarInterval(); TRawArray& array = *fValues; unsigned long valueIndex = 0; for (GraphValue value = array[valueIndex]; valueIndex < array.GetNumberOfValues(); value = array[++valueIndex]) { if (value != graph.GetYRange().GetMin()) { SetBar(xValue, value, bar); bar.Draw(port); } xValue += GetBarInterval(); } } void TBarDrawer::SetBar(GraphValue xValue, GraphValue yValue, TArea& bar) const { TGPoint endOfBar = GetTargetGraph().MapValuesToPoint(xValue, yValue); TGPoint xAxisPoint = GetTargetGraph().MapValuesToPoint(xValue, GetXAxisLocation()); GCoordinate barLength = 0.0; TGPoint translatePoint(endOfBar); translatePoint.fX -= GetBarWidth() / 2; TGrafMatrix matrix; bar = fBar; if (yValue > GetXAxisLocation()) { barLength = xAxisPoint.fY - endOfBar.fY; } else if (yValue < GetXAxisLocation()) { barLength = endOfBar.fY - xAxisPoint.fY; translatePoint.fY = xAxisPoint.fY; } matrix.SetToScale(TGPoint(GetBarWidth(), barLength)); matrix.TranslateBy(translatePoint); bar.TransformBy(matrix); } void TBarDrawer::SetXAxisLocation(GraphValue where) { fXAxisLocation = where; } GraphValue TBarDrawer::GetXAxisLocation() const { return fXAxisLocation; } TGRect TBarDrawer::GetBounds() const { TArea bar(fBar); TGRect totalBounds(TGRect::kZeroRect); GraphValue xValue = GetTargetGraph().GetXRange().GetMin() + GetBarInterval(); TRawArray& array = *fValues; unsigned long valueIndex = 0; for (GraphValue value = array[valueIndex]; valueIndex < array.GetNumberOfValues(); ++valueIndex) { SetBar(xValue, value, bar); totalBounds.ExtendTo(bar.GetGeometricBounds()); xValue += GetBarInterval(); } return totalBounds; }