private static string FitDateTime(IRenderDevice context, double dAvailableWidth, DateTime datetime, IEnumerable <string> dateTimeFormats, out double dFoundWidth) { dFoundWidth = 0; foreach (string format in dateTimeFormats) { string sText = datetime.ToString(format); double textWidth = context.GetTextWidth(sText); if (textWidth >= dAvailableWidth) { continue; } dFoundWidth = textWidth; return(sText); } return(string.Empty); }
public void PaintEx(IRenderDevice context) { if (!m_bChanged || m_dtStartTime == DateTime.MinValue || m_dtStartTime == DateTime.MaxValue) { return; } double iSymbolWidth = context.GetTextWidth("W") * 1.2; //find the minimum possible panel format MinPanel = null; double dAvailableWidth = context.AvailableWidth; DateTime curDateTime; foreach (PanelFormat panel in panels) { //round start and end-time accordingly to panel format ChangedStartTime = panel.RoundStartDateTime(m_dtStartTime); ChangedEndTime = panel.RoundEndDateTime(m_dtEndTime); curDateTime = ChangedStartTime; double dPanelLen = 0; while (dPanelLen <= dAvailableWidth && curDateTime <= ChangedEndTime) { dPanelLen += panel.SymbolsPerUnit * iSymbolWidth; curDateTime = panel.IncreaseTime(curDateTime); } if (dPanelLen > dAvailableWidth) { continue; } MinPanel = panel; break; } if (MinPanel == null) { return; } context.PlotPanelSeparator(0); double dTotalSeconds = (ChangedEndTime - ChangedStartTime).TotalSeconds; _plotingUnitWidth = dAvailableWidth / dTotalSeconds; double dx; string sText; //main panel curDateTime = ChangedStartTime; if (m_datesCargo.Count == 0) //even scale { for (; curDateTime <= ChangedEndTime; curDateTime = MinPanel.IncreaseTime(curDateTime)) { dx = (curDateTime - ChangedStartTime).TotalSeconds * _plotingUnitWidth; context.PlotUnitSeparator((float)dx, true, 0); sText = MinPanel.FormatDateTime(curDateTime); if (curDateTime < ChangedEndTime && dx + context.GetTextWidth(sText) < dAvailableWidth) { context.PlotUnitText((float)dx, sText, 0); } } } else { double dTotalCargo = 0; foreach (KeyValuePair <DateTime, double> pair in m_datesCargo) { dTotalCargo += pair.Value; } double dPrevTotalWidth = 0; foreach (KeyValuePair <DateTime, double> pair in m_datesCargo) { double dUnitCargo = pair.Value; double dUnitWidth = (dUnitCargo * dAvailableWidth) / dTotalCargo; if (MinPanel.SymbolsPerUnit * iSymbolWidth > dUnitWidth) { continue; } context.PlotUnitSeparator((float)dPrevTotalWidth, true, 0); context.PlotUnitText((float)dPrevTotalWidth, MinPanel.FormatDateTime(pair.Key), 0); dPrevTotalWidth += dUnitWidth; } } //Sub Panel painting if (MinPanel.DateFormats == null || MinPanel.GetNextSubPanelSeparator == null) { return; } context.PlotPanelSeparator(1); curDateTime = ChangedStartTime; double firstWidth = 0; DateTime prevSeparator = DateTime.MinValue; for (; curDateTime <= ChangedEndTime; curDateTime = MinPanel.GetNextSubPanelSeparator(curDateTime)) { if (curDateTime == ChangedStartTime) //at the first position draw the longest possible, this my overlay some of next ones, they must be ignored { context.PlotUnitSeparator(0, true, 1); if ((sText = FitDateTime(context, dAvailableWidth, curDateTime, MinPanel.DateFormats, out firstWidth)) != string.Empty) { context.PlotUnitText(0, sText, 1); } } else { //if (!MinPanel.IsSubPanelSeparator(curDateTime)) continue; dx = (curDateTime - ChangedStartTime).TotalSeconds * _plotingUnitWidth; if (dx <= firstWidth) { continue; //ignore those separatoes that are overlayed by first one } context.PlotUnitSeparator((float)dx, true, 1); if (prevSeparator == DateTime.MinValue) { prevSeparator = curDateTime; continue; } PlotSubPanelText(context, dAvailableWidth, curDateTime, _plotingUnitWidth, out firstWidth, prevSeparator); prevSeparator = curDateTime;//remember last position } } if (prevSeparator == DateTime.MinValue) { return; } PlotSubPanelText(context, dAvailableWidth, curDateTime, _plotingUnitWidth, out firstWidth, prevSeparator); }