mirror of
https://github.com/OPM/ResInsight.git
synced 2025-02-25 18:55:39 -06:00
#8611 QtChart : Improve time axis grid intervals
Use QCatetoryAxis to display date labels Use Qwt label formatting and scale dividing tools to produce text labels
This commit is contained in:
parent
721ba508de
commit
ceecf2e54c
@ -99,6 +99,7 @@ set(SOURCE_GROUP_HEADER_FILES
|
|||||||
${CMAKE_CURRENT_LIST_DIR}/RiuFileDialogTools.h
|
${CMAKE_CURRENT_LIST_DIR}/RiuFileDialogTools.h
|
||||||
${CMAKE_CURRENT_LIST_DIR}/RiuGuiTheme.h
|
${CMAKE_CURRENT_LIST_DIR}/RiuGuiTheme.h
|
||||||
${CMAKE_CURRENT_LIST_DIR}/RiuQssSyntaxHighlighter.h
|
${CMAKE_CURRENT_LIST_DIR}/RiuQssSyntaxHighlighter.h
|
||||||
|
${CMAKE_CURRENT_LIST_DIR}/RiuQwtDateScaleWrapper.h
|
||||||
)
|
)
|
||||||
|
|
||||||
set(SOURCE_GROUP_SOURCE_FILES
|
set(SOURCE_GROUP_SOURCE_FILES
|
||||||
@ -199,6 +200,7 @@ set(SOURCE_GROUP_SOURCE_FILES
|
|||||||
${CMAKE_CURRENT_LIST_DIR}/RiuQssSyntaxHighlighter.cpp
|
${CMAKE_CURRENT_LIST_DIR}/RiuQssSyntaxHighlighter.cpp
|
||||||
${CMAKE_CURRENT_LIST_DIR}/RiuTextEditWithCompletion.cpp
|
${CMAKE_CURRENT_LIST_DIR}/RiuTextEditWithCompletion.cpp
|
||||||
${CMAKE_CURRENT_LIST_DIR}/RiuTextContentFrame.cpp
|
${CMAKE_CURRENT_LIST_DIR}/RiuTextContentFrame.cpp
|
||||||
|
${CMAKE_CURRENT_LIST_DIR}/RiuQwtDateScaleWrapper.cpp
|
||||||
)
|
)
|
||||||
|
|
||||||
if(RESINSIGHT_USE_QT_CHARTS)
|
if(RESINSIGHT_USE_QT_CHARTS)
|
||||||
|
@ -31,12 +31,13 @@
|
|||||||
#include "RiuPlotWidget.h"
|
#include "RiuPlotWidget.h"
|
||||||
#include "RiuQtChartView.h"
|
#include "RiuQtChartView.h"
|
||||||
#include "RiuQtChartsPlotCurve.h"
|
#include "RiuQtChartsPlotCurve.h"
|
||||||
|
#include "RiuQwtDateScaleWrapper.h"
|
||||||
|
|
||||||
#include "cafAssert.h"
|
#include "cafAssert.h"
|
||||||
|
|
||||||
#include "cvfTrace.h"
|
#include "cvfTrace.h"
|
||||||
|
|
||||||
#include <QDateTimeAxis>
|
#include <QCategoryAxis>
|
||||||
#include <QGraphicsLayout>
|
#include <QGraphicsLayout>
|
||||||
#include <QLogValueAxis>
|
#include <QLogValueAxis>
|
||||||
#include <QVBoxLayout>
|
#include <QVBoxLayout>
|
||||||
@ -52,6 +53,7 @@ using namespace QtCharts;
|
|||||||
//--------------------------------------------------------------------------------------------------
|
//--------------------------------------------------------------------------------------------------
|
||||||
RiuQtChartsPlotWidget::RiuQtChartsPlotWidget( RimPlot* plotDefinition, QWidget* parent )
|
RiuQtChartsPlotWidget::RiuQtChartsPlotWidget( RimPlot* plotDefinition, QWidget* parent )
|
||||||
: RiuPlotWidget( plotDefinition, parent )
|
: RiuPlotWidget( plotDefinition, parent )
|
||||||
|
, m_dateScaleWrapper( new RiuQwtDateScaleWrapper() )
|
||||||
{
|
{
|
||||||
CAF_ASSERT( m_plotDefinition );
|
CAF_ASSERT( m_plotDefinition );
|
||||||
|
|
||||||
@ -59,7 +61,7 @@ RiuQtChartsPlotWidget::RiuQtChartsPlotWidget( RimPlot* plotDefinition, QWidget*
|
|||||||
layout->setContentsMargins( 0, 0, 0, 0 );
|
layout->setContentsMargins( 0, 0, 0, 0 );
|
||||||
setLayout( layout );
|
setLayout( layout );
|
||||||
|
|
||||||
QtCharts::QChart* chart = new QtCharts::QChart();
|
QChart* chart = new QChart();
|
||||||
chart->layout()->setContentsMargins( 0, 0, 0, 0 );
|
chart->layout()->setContentsMargins( 0, 0, 0, 0 );
|
||||||
chart->setBackgroundRoundness( 0 );
|
chart->setBackgroundRoundness( 0 );
|
||||||
chart->setAcceptDrops( true );
|
chart->setAcceptDrops( true );
|
||||||
@ -90,6 +92,9 @@ RiuQtChartsPlotWidget::~RiuQtChartsPlotWidget()
|
|||||||
{
|
{
|
||||||
m_plotDefinition->detachAllCurves();
|
m_plotDefinition->detachAllCurves();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
delete m_dateScaleWrapper;
|
||||||
|
m_dateScaleWrapper = nullptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
//--------------------------------------------------------------------------------------------------
|
//--------------------------------------------------------------------------------------------------
|
||||||
@ -97,6 +102,25 @@ RiuQtChartsPlotWidget::~RiuQtChartsPlotWidget()
|
|||||||
//--------------------------------------------------------------------------------------------------
|
//--------------------------------------------------------------------------------------------------
|
||||||
void RiuQtChartsPlotWidget::axisRangeChanged()
|
void RiuQtChartsPlotWidget::axisRangeChanged()
|
||||||
{
|
{
|
||||||
|
auto catAxis = categoryAxis();
|
||||||
|
if ( catAxis )
|
||||||
|
{
|
||||||
|
auto min = catAxis->min();
|
||||||
|
auto max = catAxis->max();
|
||||||
|
|
||||||
|
auto existingLabels = catAxis->categoriesLabels();
|
||||||
|
for ( const auto& l : existingLabels )
|
||||||
|
{
|
||||||
|
catAxis->remove( l );
|
||||||
|
}
|
||||||
|
|
||||||
|
auto positionLabel = m_dateScaleWrapper->positionsAndLabels( min, max );
|
||||||
|
for ( auto [pos, label] : positionLabel )
|
||||||
|
{
|
||||||
|
catAxis->append( label, pos );
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if ( qtChart()->isZoomed() ) emit plotZoomed();
|
if ( qtChart()->isZoomed() ) emit plotZoomed();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -158,7 +182,7 @@ void RiuQtChartsPlotWidget::setAxisFontsAndAlignment( RiuPlotAxis axis,
|
|||||||
//--------------------------------------------------------------------------------------------------
|
//--------------------------------------------------------------------------------------------------
|
||||||
void RiuQtChartsPlotWidget::setAxesFontsAndAlignment( int titleFontSize, int valueFontSize, bool titleBold, int alignment )
|
void RiuQtChartsPlotWidget::setAxesFontsAndAlignment( int titleFontSize, int valueFontSize, bool titleBold, int alignment )
|
||||||
{
|
{
|
||||||
for ( auto axisTitlePair : m_axisTitles )
|
for ( const auto& axisTitlePair : m_axisTitles )
|
||||||
{
|
{
|
||||||
setAxisFontsAndAlignment( axisTitlePair.first, titleFontSize, valueFontSize, titleBold, alignment );
|
setAxisFontsAndAlignment( axisTitlePair.first, titleFontSize, valueFontSize, titleBold, alignment );
|
||||||
}
|
}
|
||||||
@ -194,9 +218,6 @@ void RiuQtChartsPlotWidget::setAxisFormat( RiuPlotAxis axis, const QString& form
|
|||||||
|
|
||||||
auto logAxis = dynamic_cast<QLogValueAxis*>( ax );
|
auto logAxis = dynamic_cast<QLogValueAxis*>( ax );
|
||||||
if ( logAxis ) logAxis->setLabelFormat( format );
|
if ( logAxis ) logAxis->setLabelFormat( format );
|
||||||
|
|
||||||
auto dateAxis = dynamic_cast<QDateTimeAxis*>( ax );
|
|
||||||
if ( dateAxis ) dateAxis->setFormat( format );
|
|
||||||
}
|
}
|
||||||
|
|
||||||
//--------------------------------------------------------------------------------------------------
|
//--------------------------------------------------------------------------------------------------
|
||||||
@ -319,9 +340,6 @@ std::pair<double, double> RiuQtChartsPlotWidget::axisRange( RiuPlotAxis axis ) c
|
|||||||
auto logAxis = dynamic_cast<QLogValueAxis*>( ax );
|
auto logAxis = dynamic_cast<QLogValueAxis*>( ax );
|
||||||
if ( logAxis ) return std::make_pair( logAxis->min(), logAxis->max() );
|
if ( logAxis ) return std::make_pair( logAxis->min(), logAxis->max() );
|
||||||
|
|
||||||
auto dateAxis = dynamic_cast<QDateTimeAxis*>( ax );
|
|
||||||
if ( dateAxis ) return std::make_pair( dateAxis->min().toMSecsSinceEpoch(), dateAxis->max().toMSecsSinceEpoch() );
|
|
||||||
|
|
||||||
return std::make_pair( 0.0, 1.0 );
|
return std::make_pair( 0.0, 1.0 );
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -612,6 +630,20 @@ void RiuQtChartsPlotWidget::replot()
|
|||||||
qtChart()->update();
|
qtChart()->update();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//--------------------------------------------------------------------------------------------------
|
||||||
|
///
|
||||||
|
//--------------------------------------------------------------------------------------------------
|
||||||
|
QCategoryAxis* RiuQtChartsPlotWidget::categoryAxis()
|
||||||
|
{
|
||||||
|
for ( const auto& a : m_axes )
|
||||||
|
{
|
||||||
|
auto catAxis = dynamic_cast<QCategoryAxis*>( a.second );
|
||||||
|
if ( catAxis ) return catAxis;
|
||||||
|
}
|
||||||
|
|
||||||
|
return nullptr;
|
||||||
|
}
|
||||||
|
|
||||||
//--------------------------------------------------------------------------------------------------
|
//--------------------------------------------------------------------------------------------------
|
||||||
///
|
///
|
||||||
//--------------------------------------------------------------------------------------------------
|
//--------------------------------------------------------------------------------------------------
|
||||||
@ -624,6 +656,17 @@ void RiuQtChartsPlotWidget::updateZoomDependentCurveProperties()
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//--------------------------------------------------------------------------------------------------
|
||||||
|
///
|
||||||
|
//--------------------------------------------------------------------------------------------------
|
||||||
|
void RiuQtChartsPlotWidget::setFormatStrings( const QString& dateFormat,
|
||||||
|
const QString& timeFormat,
|
||||||
|
RiaQDateTimeTools::DateFormatComponents dateComponents,
|
||||||
|
RiaQDateTimeTools::TimeFormatComponents timeComponents )
|
||||||
|
{
|
||||||
|
m_dateScaleWrapper->setFormatStrings( dateFormat, timeFormat, dateComponents, timeComponents );
|
||||||
|
}
|
||||||
|
|
||||||
//--------------------------------------------------------------------------------------------------
|
//--------------------------------------------------------------------------------------------------
|
||||||
///
|
///
|
||||||
//--------------------------------------------------------------------------------------------------
|
//--------------------------------------------------------------------------------------------------
|
||||||
@ -671,8 +714,7 @@ void RiuQtChartsPlotWidget::setAxisMaxMajor( RiuPlotAxis axis, int maxMajor )
|
|||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
QDateTimeAxis* dateAxis = dynamic_cast<QDateTimeAxis*>( ax );
|
m_dateScaleWrapper->setMaxMajorTicks( maxMajor );
|
||||||
if ( dateAxis ) dateAxis->setTickCount( maxMajor );
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -703,16 +745,9 @@ void RiuQtChartsPlotWidget::setAxisAutoScale( RiuPlotAxis axis, bool autoScale )
|
|||||||
///
|
///
|
||||||
//--------------------------------------------------------------------------------------------------
|
//--------------------------------------------------------------------------------------------------
|
||||||
void RiuQtChartsPlotWidget::setAxisScale( RiuPlotAxis axis, double min, double max )
|
void RiuQtChartsPlotWidget::setAxisScale( RiuPlotAxis axis, double min, double max )
|
||||||
{
|
|
||||||
if ( axisScaleType( axis ) == RiuPlotWidget::AxisScaleType::DATE )
|
|
||||||
{
|
|
||||||
plotAxis( axis )->setRange( QDateTime::fromMSecsSinceEpoch( min ), QDateTime::fromMSecsSinceEpoch( max ) );
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
{
|
||||||
plotAxis( axis )->setRange( min, max );
|
plotAxis( axis )->setRange( min, max );
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
//--------------------------------------------------------------------------------------------------
|
//--------------------------------------------------------------------------------------------------
|
||||||
///
|
///
|
||||||
@ -738,7 +773,9 @@ void RiuQtChartsPlotWidget::setAxisScaleType( RiuPlotAxis axis, RiuQtChartsPlotW
|
|||||||
}
|
}
|
||||||
else if ( axisScaleType == AxisScaleType::DATE )
|
else if ( axisScaleType == AxisScaleType::DATE )
|
||||||
{
|
{
|
||||||
insertaxis = new QDateTimeAxis;
|
auto categoryAxis = new QCategoryAxis;
|
||||||
|
categoryAxis->setLabelsPosition( QCategoryAxis::AxisLabelsPosition::AxisLabelsPositionOnValue );
|
||||||
|
insertaxis = categoryAxis;
|
||||||
}
|
}
|
||||||
else if ( axisScaleType == AxisScaleType::LINEAR )
|
else if ( axisScaleType == AxisScaleType::LINEAR )
|
||||||
{
|
{
|
||||||
@ -780,7 +817,7 @@ RiuPlotCurve* RiuQtChartsPlotWidget::createPlotCurve( RimPlotCurve* ownerRimCurv
|
|||||||
//--------------------------------------------------------------------------------------------------
|
//--------------------------------------------------------------------------------------------------
|
||||||
///
|
///
|
||||||
//--------------------------------------------------------------------------------------------------
|
//--------------------------------------------------------------------------------------------------
|
||||||
QtCharts::QChart* RiuQtChartsPlotWidget::qtChart()
|
QChart* RiuQtChartsPlotWidget::qtChart()
|
||||||
{
|
{
|
||||||
return m_viewer->chart();
|
return m_viewer->chart();
|
||||||
}
|
}
|
||||||
@ -789,12 +826,12 @@ QtCharts::QChart* RiuQtChartsPlotWidget::qtChart()
|
|||||||
///
|
///
|
||||||
//--------------------------------------------------------------------------------------------------
|
//--------------------------------------------------------------------------------------------------
|
||||||
void RiuQtChartsPlotWidget::attach( RiuPlotCurve* plotCurve,
|
void RiuQtChartsPlotWidget::attach( RiuPlotCurve* plotCurve,
|
||||||
QtCharts::QAbstractSeries* lineSeries,
|
QAbstractSeries* lineSeries,
|
||||||
QtCharts::QAbstractSeries* scatterSeries,
|
QAbstractSeries* scatterSeries,
|
||||||
RiuPlotAxis xAxis,
|
RiuPlotAxis xAxis,
|
||||||
RiuPlotAxis yAxis )
|
RiuPlotAxis yAxis )
|
||||||
{
|
{
|
||||||
auto addToChart = [this]( std::map<RiuPlotCurve*, QtCharts::QAbstractSeries*>& curveSeriesMap,
|
auto addToChart = [this]( std::map<RiuPlotCurve*, QAbstractSeries*>& curveSeriesMap,
|
||||||
auto plotCurve,
|
auto plotCurve,
|
||||||
auto series,
|
auto series,
|
||||||
auto xAxis,
|
auto xAxis,
|
||||||
@ -826,7 +863,7 @@ void RiuQtChartsPlotWidget::detach( RiuPlotCurve* plotCurve )
|
|||||||
//--------------------------------------------------------------------------------------------------
|
//--------------------------------------------------------------------------------------------------
|
||||||
///
|
///
|
||||||
//--------------------------------------------------------------------------------------------------
|
//--------------------------------------------------------------------------------------------------
|
||||||
QtCharts::QAbstractSeries* RiuQtChartsPlotWidget::getLineSeries( const RiuPlotCurve* plotCurve ) const
|
QAbstractSeries* RiuQtChartsPlotWidget::getLineSeries( const RiuPlotCurve* plotCurve ) const
|
||||||
{
|
{
|
||||||
auto series = m_lineSeriesMap.find( const_cast<RiuPlotCurve*>( plotCurve ) );
|
auto series = m_lineSeriesMap.find( const_cast<RiuPlotCurve*>( plotCurve ) );
|
||||||
if ( series != m_lineSeriesMap.end() )
|
if ( series != m_lineSeriesMap.end() )
|
||||||
@ -838,7 +875,7 @@ QtCharts::QAbstractSeries* RiuQtChartsPlotWidget::getLineSeries( const RiuPlotCu
|
|||||||
//--------------------------------------------------------------------------------------------------
|
//--------------------------------------------------------------------------------------------------
|
||||||
///
|
///
|
||||||
//--------------------------------------------------------------------------------------------------
|
//--------------------------------------------------------------------------------------------------
|
||||||
QtCharts::QAbstractSeries* RiuQtChartsPlotWidget::getScatterSeries( const RiuPlotCurve* plotCurve ) const
|
QAbstractSeries* RiuQtChartsPlotWidget::getScatterSeries( const RiuPlotCurve* plotCurve ) const
|
||||||
{
|
{
|
||||||
auto series = m_scatterSeriesMap.find( const_cast<RiuPlotCurve*>( plotCurve ) );
|
auto series = m_scatterSeriesMap.find( const_cast<RiuPlotCurve*>( plotCurve ) );
|
||||||
if ( series != m_scatterSeriesMap.end() )
|
if ( series != m_scatterSeriesMap.end() )
|
||||||
@ -869,7 +906,7 @@ void RiuQtChartsPlotWidget::detachItems( RiuPlotWidget::PlotItemType plotItemTyp
|
|||||||
//--------------------------------------------------------------------------------------------------
|
//--------------------------------------------------------------------------------------------------
|
||||||
///
|
///
|
||||||
//--------------------------------------------------------------------------------------------------
|
//--------------------------------------------------------------------------------------------------
|
||||||
void RiuQtChartsPlotWidget::setXAxis( RiuPlotAxis axis, QtCharts::QAbstractSeries* series, RiuQtChartsPlotCurve* plotCurve )
|
void RiuQtChartsPlotWidget::setXAxis( RiuPlotAxis axis, QAbstractSeries* series, RiuQtChartsPlotCurve* plotCurve )
|
||||||
{
|
{
|
||||||
attachSeriesToAxis( axis, series, plotCurve );
|
attachSeriesToAxis( axis, series, plotCurve );
|
||||||
}
|
}
|
||||||
@ -877,7 +914,7 @@ void RiuQtChartsPlotWidget::setXAxis( RiuPlotAxis axis, QtCharts::QAbstractSerie
|
|||||||
//--------------------------------------------------------------------------------------------------
|
//--------------------------------------------------------------------------------------------------
|
||||||
///
|
///
|
||||||
//--------------------------------------------------------------------------------------------------
|
//--------------------------------------------------------------------------------------------------
|
||||||
void RiuQtChartsPlotWidget::setYAxis( RiuPlotAxis axis, QtCharts::QAbstractSeries* series, RiuQtChartsPlotCurve* plotCurve )
|
void RiuQtChartsPlotWidget::setYAxis( RiuPlotAxis axis, QAbstractSeries* series, RiuQtChartsPlotCurve* plotCurve )
|
||||||
{
|
{
|
||||||
attachSeriesToAxis( axis, series, plotCurve );
|
attachSeriesToAxis( axis, series, plotCurve );
|
||||||
}
|
}
|
||||||
@ -896,9 +933,7 @@ void RiuQtChartsPlotWidget::ensureAxisIsCreated( RiuPlotAxis axis )
|
|||||||
//--------------------------------------------------------------------------------------------------
|
//--------------------------------------------------------------------------------------------------
|
||||||
///
|
///
|
||||||
//--------------------------------------------------------------------------------------------------
|
//--------------------------------------------------------------------------------------------------
|
||||||
void RiuQtChartsPlotWidget::attachSeriesToAxis( RiuPlotAxis axis,
|
void RiuQtChartsPlotWidget::attachSeriesToAxis( RiuPlotAxis axis, QAbstractSeries* series, RiuQtChartsPlotCurve* plotCurve )
|
||||||
QtCharts::QAbstractSeries* series,
|
|
||||||
RiuQtChartsPlotCurve* plotCurve )
|
|
||||||
{
|
{
|
||||||
// Make sure the axis we are about to set exists.
|
// Make sure the axis we are about to set exists.
|
||||||
ensureAxisIsCreated( axis );
|
ensureAxisIsCreated( axis );
|
||||||
@ -930,22 +965,6 @@ void RiuQtChartsPlotWidget::attachSeriesToAxis( RiuPlotAxis axis,
|
|||||||
Qt::UniqueConnection );
|
Qt::UniqueConnection );
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else if ( qobject_cast<QDateTimeAxis*>( newAxis ) )
|
|
||||||
{
|
|
||||||
connect( newAxis,
|
|
||||||
SIGNAL( rangeChanged( QDateTime, QDateTime ) ),
|
|
||||||
this,
|
|
||||||
SLOT( axisRangeChanged() ),
|
|
||||||
Qt::UniqueConnection );
|
|
||||||
if ( plotCurve )
|
|
||||||
{
|
|
||||||
connect( newAxis,
|
|
||||||
SIGNAL( rangeChanged( QDateTime, QDateTime ) ),
|
|
||||||
plotCurve,
|
|
||||||
SLOT( axisRangeChanged() ),
|
|
||||||
Qt::UniqueConnection );
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -954,7 +973,7 @@ void RiuQtChartsPlotWidget::attachSeriesToAxis( RiuPlotAxis axis,
|
|||||||
//--------------------------------------------------------------------------------------------------
|
//--------------------------------------------------------------------------------------------------
|
||||||
void RiuQtChartsPlotWidget::addAxis( RiuPlotAxis plotAxis, bool isEnabled, bool isAutoScale )
|
void RiuQtChartsPlotWidget::addAxis( RiuPlotAxis plotAxis, bool isEnabled, bool isAutoScale )
|
||||||
{
|
{
|
||||||
QValueAxis* axis = new QValueAxis();
|
auto* axis = new QValueAxis();
|
||||||
qtChart()->addAxis( axis, mapPlotAxisToQtAlignment( plotAxis.axis() ) );
|
qtChart()->addAxis( axis, mapPlotAxisToQtAlignment( plotAxis.axis() ) );
|
||||||
m_axes[plotAxis] = axis;
|
m_axes[plotAxis] = axis;
|
||||||
m_axesEnabled[plotAxis] = isEnabled;
|
m_axesEnabled[plotAxis] = isEnabled;
|
||||||
@ -967,7 +986,7 @@ void RiuQtChartsPlotWidget::addAxis( RiuPlotAxis plotAxis, bool isEnabled, bool
|
|||||||
RiuPlotAxis RiuQtChartsPlotWidget::createNextPlotAxis( RiaDefines::PlotAxis axis )
|
RiuPlotAxis RiuQtChartsPlotWidget::createNextPlotAxis( RiaDefines::PlotAxis axis )
|
||||||
{
|
{
|
||||||
int minIdx = -1;
|
int minIdx = -1;
|
||||||
for ( auto a : m_axes )
|
for ( const auto& a : m_axes )
|
||||||
{
|
{
|
||||||
if ( a.first.axis() == axis )
|
if ( a.first.axis() == axis )
|
||||||
{
|
{
|
||||||
@ -1009,18 +1028,12 @@ void RiuQtChartsPlotWidget::rescaleAxis( RiuPlotAxis axis )
|
|||||||
QVector<QPointF> points;
|
QVector<QPointF> points;
|
||||||
for ( auto attachedAxis : attachedAxes )
|
for ( auto attachedAxis : attachedAxes )
|
||||||
{
|
{
|
||||||
QValueAxis* valueAxis = dynamic_cast<QValueAxis*>( attachedAxis );
|
auto* valueAxis = dynamic_cast<QValueAxis*>( attachedAxis );
|
||||||
if ( valueAxis && valueAxis->orientation() == orr && dynamic_cast<QLineSeries*>( series ) )
|
if ( valueAxis && valueAxis->orientation() == orr && dynamic_cast<QLineSeries*>( series ) )
|
||||||
{
|
{
|
||||||
points = dynamic_cast<QLineSeries*>( series )->pointsVector();
|
points = dynamic_cast<QLineSeries*>( series )->pointsVector();
|
||||||
}
|
}
|
||||||
|
|
||||||
QDateTimeAxis* dateTimeAxis = dynamic_cast<QDateTimeAxis*>( attachedAxis );
|
|
||||||
if ( dateTimeAxis && dateTimeAxis->orientation() == orr && dynamic_cast<QLineSeries*>( series ) )
|
|
||||||
{
|
|
||||||
points = dynamic_cast<QLineSeries*>( series )->pointsVector();
|
|
||||||
}
|
|
||||||
|
|
||||||
for ( auto p : points )
|
for ( auto p : points )
|
||||||
{
|
{
|
||||||
if ( orr == Qt::Orientation::Horizontal )
|
if ( orr == Qt::Orientation::Horizontal )
|
||||||
@ -1041,9 +1054,26 @@ void RiuQtChartsPlotWidget::rescaleAxis( RiuPlotAxis axis )
|
|||||||
// Block signals to avoid triggering RimSummaryPlot::onPlotZoomed
|
// Block signals to avoid triggering RimSummaryPlot::onPlotZoomed
|
||||||
pAxis->blockSignals( true );
|
pAxis->blockSignals( true );
|
||||||
|
|
||||||
if ( axisScaleType( axis ) == RiuPlotWidget::AxisScaleType::DATE )
|
if ( axis.axis() == RiaDefines::PlotAxis::PLOT_AXIS_BOTTOM )
|
||||||
{
|
{
|
||||||
pAxis->setRange( QDateTime::fromMSecsSinceEpoch( min ), QDateTime::fromMSecsSinceEpoch( max ) );
|
auto catAxis = categoryAxis();
|
||||||
|
if ( catAxis )
|
||||||
|
{
|
||||||
|
auto existingLabels = catAxis->categoriesLabels();
|
||||||
|
for ( const auto& l : existingLabels )
|
||||||
|
{
|
||||||
|
catAxis->remove( l );
|
||||||
|
}
|
||||||
|
|
||||||
|
auto [adjustedMin, adjustedMax, tickCount] = m_dateScaleWrapper->adjustedRange( min, max );
|
||||||
|
catAxis->setRange( adjustedMin, adjustedMax );
|
||||||
|
|
||||||
|
auto positionLabel = m_dateScaleWrapper->positionsAndLabels( adjustedMin, adjustedMax );
|
||||||
|
for ( auto [pos, label] : positionLabel )
|
||||||
|
{
|
||||||
|
catAxis->append( label, pos );
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
|
@ -21,6 +21,7 @@
|
|||||||
|
|
||||||
#include "RiaDefines.h"
|
#include "RiaDefines.h"
|
||||||
#include "RiaPlotDefines.h"
|
#include "RiaPlotDefines.h"
|
||||||
|
#include "RiaQDateTimeTools.h"
|
||||||
|
|
||||||
#include "RiuPlotWidget.h"
|
#include "RiuPlotWidget.h"
|
||||||
|
|
||||||
@ -41,6 +42,7 @@ class QLabel;
|
|||||||
class QPainter;
|
class QPainter;
|
||||||
class QPaintDevice;
|
class QPaintDevice;
|
||||||
class QWheelEvent;
|
class QWheelEvent;
|
||||||
|
class RiuQwtDateScaleWrapper;
|
||||||
|
|
||||||
namespace QtCharts
|
namespace QtCharts
|
||||||
{
|
{
|
||||||
@ -49,6 +51,7 @@ class QChart;
|
|||||||
class QAbstractSeries;
|
class QAbstractSeries;
|
||||||
class QAbstractAxis;
|
class QAbstractAxis;
|
||||||
class QChartView;
|
class QChartView;
|
||||||
|
class QCategoryAxis;
|
||||||
}; // namespace QtCharts
|
}; // namespace QtCharts
|
||||||
|
|
||||||
//==================================================================================================
|
//==================================================================================================
|
||||||
@ -178,6 +181,11 @@ public:
|
|||||||
|
|
||||||
void updateZoomDependentCurveProperties() override;
|
void updateZoomDependentCurveProperties() override;
|
||||||
|
|
||||||
|
void setFormatStrings( const QString& dateFormat,
|
||||||
|
const QString& timeFormat,
|
||||||
|
RiaQDateTimeTools::DateFormatComponents dateComponents,
|
||||||
|
RiaQDateTimeTools::TimeFormatComponents timeComponents );
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
void attachSeriesToAxis( RiuPlotAxis axis, QtCharts::QAbstractSeries* series, RiuQtChartsPlotCurve* plotCurve );
|
void attachSeriesToAxis( RiuPlotAxis axis, QtCharts::QAbstractSeries* series, RiuQtChartsPlotCurve* plotCurve );
|
||||||
|
|
||||||
@ -215,6 +223,9 @@ private:
|
|||||||
static int defaultMinimumWidth();
|
static int defaultMinimumWidth();
|
||||||
void replot() override;
|
void replot() override;
|
||||||
|
|
||||||
|
QtCharts::QCategoryAxis* categoryAxis();
|
||||||
|
|
||||||
|
private:
|
||||||
QPointer<QtCharts::QChartView> m_viewer;
|
QPointer<QtCharts::QChartView> m_viewer;
|
||||||
|
|
||||||
std::map<RiuPlotAxis, QtCharts::QAbstractAxis*> m_axes;
|
std::map<RiuPlotAxis, QtCharts::QAbstractAxis*> m_axes;
|
||||||
@ -223,4 +234,6 @@ private:
|
|||||||
|
|
||||||
std::map<RiuPlotCurve*, QtCharts::QAbstractSeries*> m_lineSeriesMap;
|
std::map<RiuPlotCurve*, QtCharts::QAbstractSeries*> m_lineSeriesMap;
|
||||||
std::map<RiuPlotCurve*, QtCharts::QAbstractSeries*> m_scatterSeriesMap;
|
std::map<RiuPlotCurve*, QtCharts::QAbstractSeries*> m_scatterSeriesMap;
|
||||||
|
|
||||||
|
RiuQwtDateScaleWrapper* m_dateScaleWrapper;
|
||||||
};
|
};
|
||||||
|
126
ApplicationLibCode/UserInterface/RiuQwtDateScaleWrapper.cpp
Normal file
126
ApplicationLibCode/UserInterface/RiuQwtDateScaleWrapper.cpp
Normal file
@ -0,0 +1,126 @@
|
|||||||
|
/////////////////////////////////////////////////////////////////////////////////
|
||||||
|
//
|
||||||
|
// Copyright (C) 2022 Equinor ASA
|
||||||
|
//
|
||||||
|
// ResInsight is free software: you can redistribute it and/or modify
|
||||||
|
// it under the terms of the GNU General Public License as published by
|
||||||
|
// the Free Software Foundation, either version 3 of the License, or
|
||||||
|
// (at your option) any later version.
|
||||||
|
//
|
||||||
|
// ResInsight is distributed in the hope that it will be useful, but WITHOUT ANY
|
||||||
|
// WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||||
|
// FITNESS FOR A PARTICULAR PURPOSE.
|
||||||
|
//
|
||||||
|
// See the GNU General Public License at <http://www.gnu.org/licenses/gpl.html>
|
||||||
|
// for more details.
|
||||||
|
//
|
||||||
|
/////////////////////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
|
#include "RiuQwtDateScaleWrapper.h"
|
||||||
|
#include "RiuQwtPlotTools.h"
|
||||||
|
|
||||||
|
#include <set>
|
||||||
|
|
||||||
|
//--------------------------------------------------------------------------------------------------
|
||||||
|
///
|
||||||
|
//--------------------------------------------------------------------------------------------------
|
||||||
|
RiuQwtDateScaleWrapper::RiuQwtDateScaleWrapper()
|
||||||
|
: m_scaleEngine( Qt::UTC )
|
||||||
|
, m_maxMajorTicks( 7 )
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
//--------------------------------------------------------------------------------------------------
|
||||||
|
///
|
||||||
|
//--------------------------------------------------------------------------------------------------
|
||||||
|
void RiuQwtDateScaleWrapper::setFormatStrings( const QString& dateFormat,
|
||||||
|
const QString& timeFormat,
|
||||||
|
RiaQDateTimeTools::DateFormatComponents dateComponents,
|
||||||
|
RiaQDateTimeTools::TimeFormatComponents timeComponents )
|
||||||
|
{
|
||||||
|
std::set<QwtDate::IntervalType> intervals = { QwtDate::Year,
|
||||||
|
QwtDate::Month,
|
||||||
|
QwtDate::Week,
|
||||||
|
QwtDate::Day,
|
||||||
|
QwtDate::Hour,
|
||||||
|
QwtDate::Minute,
|
||||||
|
QwtDate::Second,
|
||||||
|
QwtDate::Millisecond };
|
||||||
|
|
||||||
|
for ( QwtDate::IntervalType interval : intervals )
|
||||||
|
{
|
||||||
|
m_scaleDraw.setDateFormat( interval,
|
||||||
|
RiuQwtPlotTools::dateTimeFormatForInterval( interval,
|
||||||
|
dateFormat,
|
||||||
|
timeFormat,
|
||||||
|
dateComponents,
|
||||||
|
timeComponents ) );
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
//--------------------------------------------------------------------------------------------------
|
||||||
|
///
|
||||||
|
//--------------------------------------------------------------------------------------------------
|
||||||
|
void RiuQwtDateScaleWrapper::setMaxMajorTicks( int tickCount )
|
||||||
|
{
|
||||||
|
m_maxMajorTicks = tickCount;
|
||||||
|
}
|
||||||
|
|
||||||
|
//--------------------------------------------------------------------------------------------------
|
||||||
|
///
|
||||||
|
//--------------------------------------------------------------------------------------------------
|
||||||
|
std::tuple<double, double, int> RiuQwtDateScaleWrapper::adjustedRange( const double& min, const double& max ) const
|
||||||
|
{
|
||||||
|
double stepSize = 0.0;
|
||||||
|
|
||||||
|
double adjustedMin = min;
|
||||||
|
double adjustedMax = max;
|
||||||
|
m_scaleEngine.autoScale( m_maxMajorTicks, adjustedMin, adjustedMax, stepSize );
|
||||||
|
|
||||||
|
auto scaleDiv = m_scaleEngine.divideScale( adjustedMin, adjustedMax, m_maxMajorTicks, 0 );
|
||||||
|
auto ticks = scaleDiv.ticks( QwtScaleDiv::MajorTick );
|
||||||
|
|
||||||
|
return { adjustedMin, adjustedMax, ticks.size() - 1 };
|
||||||
|
}
|
||||||
|
|
||||||
|
//--------------------------------------------------------------------------------------------------
|
||||||
|
///
|
||||||
|
//--------------------------------------------------------------------------------------------------
|
||||||
|
QString RiuQwtDateScaleWrapper::formatStringForRange( const QDateTime& min, const QDateTime& max )
|
||||||
|
{
|
||||||
|
auto intervalType = m_scaleEngine.intervalType( min, max, m_maxMajorTicks );
|
||||||
|
auto dateFormat = m_scaleDraw.dateFormat( intervalType );
|
||||||
|
|
||||||
|
return dateFormat;
|
||||||
|
}
|
||||||
|
|
||||||
|
//--------------------------------------------------------------------------------------------------
|
||||||
|
///
|
||||||
|
//--------------------------------------------------------------------------------------------------
|
||||||
|
std::vector<std::pair<double, QString>> RiuQwtDateScaleWrapper::positionsAndLabels( const double& min, const double& max )
|
||||||
|
{
|
||||||
|
double stepSize = 0.0;
|
||||||
|
|
||||||
|
double adjustedMin = min;
|
||||||
|
double adjustedMax = max;
|
||||||
|
m_scaleEngine.autoScale( m_maxMajorTicks, adjustedMin, adjustedMax, stepSize );
|
||||||
|
|
||||||
|
auto scaleDiv = m_scaleEngine.divideScale( adjustedMin, adjustedMax, m_maxMajorTicks, 0 );
|
||||||
|
auto ticks = scaleDiv.ticks( QwtScaleDiv::MajorTick );
|
||||||
|
|
||||||
|
m_scaleDraw.setScaleDiv( scaleDiv );
|
||||||
|
|
||||||
|
auto formatString =
|
||||||
|
formatStringForRange( QDateTime::fromMSecsSinceEpoch( min ), QDateTime::fromMSecsSinceEpoch( max ) );
|
||||||
|
|
||||||
|
std::vector<std::pair<double, QString>> valueAndLabel;
|
||||||
|
for ( auto t : ticks )
|
||||||
|
{
|
||||||
|
auto qwtLabel = m_scaleDraw.label( t );
|
||||||
|
auto labelText = QDateTime::fromMSecsSinceEpoch( t ).toString( formatString );
|
||||||
|
|
||||||
|
valueAndLabel.emplace_back( t, labelText );
|
||||||
|
}
|
||||||
|
|
||||||
|
return valueAndLabel;
|
||||||
|
}
|
45
ApplicationLibCode/UserInterface/RiuQwtDateScaleWrapper.h
Normal file
45
ApplicationLibCode/UserInterface/RiuQwtDateScaleWrapper.h
Normal file
@ -0,0 +1,45 @@
|
|||||||
|
/////////////////////////////////////////////////////////////////////////////////
|
||||||
|
//
|
||||||
|
// Copyright (C) 2022 Equinor ASA
|
||||||
|
//
|
||||||
|
// ResInsight is free software: you can redistribute it and/or modify
|
||||||
|
// it under the terms of the GNU General Public License as published by
|
||||||
|
// the Free Software Foundation, either version 3 of the License, or
|
||||||
|
// (at your option) any later version.
|
||||||
|
//
|
||||||
|
// ResInsight is distributed in the hope that it will be useful, but WITHOUT ANY
|
||||||
|
// WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||||
|
// FITNESS FOR A PARTICULAR PURPOSE.
|
||||||
|
//
|
||||||
|
// See the GNU General Public License at <http://www.gnu.org/licenses/gpl.html>
|
||||||
|
// for more details.
|
||||||
|
//
|
||||||
|
/////////////////////////////////////////////////////////////////////////////////
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include "RiaQDateTimeTools.h"
|
||||||
|
|
||||||
|
#include <qwt_date_scale_draw.h>
|
||||||
|
#include <qwt_date_scale_engine.h>
|
||||||
|
|
||||||
|
class RiuQwtDateScaleWrapper
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
RiuQwtDateScaleWrapper();
|
||||||
|
|
||||||
|
void setFormatStrings( const QString& dateFormat,
|
||||||
|
const QString& timeFormat,
|
||||||
|
RiaQDateTimeTools::DateFormatComponents dateComponents,
|
||||||
|
RiaQDateTimeTools::TimeFormatComponents timeComponents );
|
||||||
|
|
||||||
|
void setMaxMajorTicks( int tickCount );
|
||||||
|
QString formatStringForRange( const QDateTime& min, const QDateTime& max );
|
||||||
|
|
||||||
|
std::tuple<double, double, int> adjustedRange( const double& min, const double& max ) const;
|
||||||
|
std::vector<std::pair<double, QString>> positionsAndLabels( const double& min, const double& max );
|
||||||
|
|
||||||
|
private:
|
||||||
|
QwtDateScaleDraw m_scaleDraw;
|
||||||
|
QwtDateScaleEngine m_scaleEngine;
|
||||||
|
int m_maxMajorTicks;
|
||||||
|
};
|
@ -62,7 +62,7 @@ void RiuSummaryQtChartsPlot::useDateBasedTimeAxis( const QString&
|
|||||||
RiaQDateTimeTools::TimeFormatComponents timeComponents )
|
RiaQDateTimeTools::TimeFormatComponents timeComponents )
|
||||||
{
|
{
|
||||||
m_plotWidget->setAxisScaleType( RiuPlotAxis::defaultBottom(), RiuPlotWidget::AxisScaleType::DATE );
|
m_plotWidget->setAxisScaleType( RiuPlotAxis::defaultBottom(), RiuPlotWidget::AxisScaleType::DATE );
|
||||||
RiuQtChartsPlotTools::enableDateBasedBottomXAxis( m_plotWidget, dateFormat, timeFormat, dateComponents, timeComponents );
|
m_plotWidget->setFormatStrings( dateFormat, timeFormat, dateComponents, timeComponents );
|
||||||
}
|
}
|
||||||
|
|
||||||
//--------------------------------------------------------------------------------------------------
|
//--------------------------------------------------------------------------------------------------
|
||||||
|
Loading…
Reference in New Issue
Block a user