// $Revision: 1.10 $ // ViewerStationery.C // Copyright (C) 1994, 1995 Taligent, Inc. All rights reserved. #ifndef TaligentSamples_VIEWERSTATIONERY #include "ViewerStationery.h" #endif #ifndef TaligentSamples_VIEWERPRESENTERSTATE #include "ViewerPresenterState.h" #endif #ifndef TaligentSamples_VIEWERCONSTRUCTORS #include "ViewerConstructors.h" #endif #ifndef TaligentSamples_LOCALSTOCKDATA #include #endif #ifndef TaligentSamples_ATOMICMODEL #include #endif #ifndef Taligent_DATETIME #include #endif #ifndef Taligent_NUMERICS #include #endif #ifndef Taligent_RANDOMNUMBER #include #endif TaligentTypeExtensionMacro(TViewerStationery); TViewerStationery::TViewerStationery() : TDocumentComponentStationery() { } TViewerStationery::TViewerStationery(const TViewerStationery& source) : TDocumentComponentStationery(source) { } TViewerStationery::~TViewerStationery() { } TModel* TViewerStationery::CreateModel() { return new TAtomicModelOn(CreateDefaultStockData()); } TModelPresenterState* TViewerStationery::CreateModelPresenterState() { TViewerPresenterState* presenterState = new TViewerPresenterState; presenterState->AdoptGraphConstructor(CreateDefaultStockGraphConstructor()); return presenterState; } TStockData* TViewerStationery::CreateDefaultStockData() const { static const TCalendar::DateTimeFieldValue kYear = 1994; static const TCalendar::DateTimeFieldValue kMonth = 1; static const TCalendar::DateTimeFieldValue kDay = 1; static const short kSampleNumberOfDays = 100; static const double kMaximumRandomNumber = 100.0; static const double kMinimumRandomNumber = 0.0; static const double kShift = 5.0; TGregorianCalendar calendar; calendar.SetField(TCalendar::kYearInEra, kYear); calendar.SetField(TCalendar::kMonthInYear, kMonth); calendar.SetField(TCalendar::kDayInMonth, kDay); TDays day; calendar.GetTime(day); TDeleterFor > stockInfo = new TDequeOf(new TOperatorComparator, new TMonomorphicStreamer); // Start with fixed low. StockValue low = 15.0; try { TUniformRandomInteger rand; for (short count = 0; count < kSampleNumberOfDays; count++) { low = GetRandomLong(rand, Max(low - kShift, kMinimumRandomNumber), Min(low + kShift, kMaximumRandomNumber)); StockValue high = GetRandomLong(rand, low + 1, Min(low + kShift, kMaximumRandomNumber - 1)); StockValue close = GetRandomLong(rand, low, high); StockValue volume = 10000.0 * close; stockInfo->Add(new TStockDay(day, high, low, close, volume)); day = day.GetDays() + 1; } } catch (...) { stockInfo->DeleteAll(); throw; } TDeleterFor data = new TLocalStockData("Sample"); data->AdoptData(stockInfo.OrphanObject()); return data.OrphanObject(); } TStockGraphConstructor* TViewerStationery::CreateDefaultStockGraphConstructor() const { return new TAllDaysPriceGraphConstructor("Price/AllDays", "Day", "Price"); } long TViewerStationery::GetRandomLong( TRandomInteger& generator, long min, long max) const { return (generator.Next() % (max - min)) + min; }