Skip to content

Commit

Permalink
FIXed drawing of secondary axes (distances betweeen axes were wrong, …
Browse files Browse the repository at this point in the history
…in part doubled and inside-ticks for secondary axes were not taken into account ... also fixed several minor bugs around this)
  • Loading branch information
jkriege2 committed Jan 10, 2024
1 parent fe6ac0e commit 4cd8a46
Show file tree
Hide file tree
Showing 44 changed files with 159 additions and 55 deletions.
Binary file modified doc/images/JKQTBasePlotter_addSecondaryXAxis.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified doc/images/JKQTBasePlotter_addSecondaryYAxis.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified doc/images/JKQTPColumnContourPlot.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified doc/images/JKQTPColumnContourPlot_small.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified doc/images/jkqtmathtext/jkqtmathtext_unicode.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified doc/images/jkqtmathtext/jkqtmathtext_utf8.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified doc/images/labelstyles/JKQTPLabelMaxBesides.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified doc/images/labelstyles/JKQTPLabelMinBesides.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified doc/images/labelstyles/JKQTPLabelMin_zeroaxis.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified doc/images/styles/blackandwhite.ini.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified doc/images/styles/blueprint.ini.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified doc/images/styles/cyberpunk.ini.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified doc/images/styles/dark.ini.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified doc/images/styles/default.ini.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified doc/images/styles/legacy_default_style.ini.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified doc/images/styles/seaborn.ini.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified doc/images/styles/simple_arrowsaxes.ini.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified doc/images/styles/simple_axesoffset.ini.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified doc/images/styles/simple_axesoffset_plotbox.ini.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified doc/images/styles/simple_gridandticks.ini.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
34 changes: 29 additions & 5 deletions examples/second_axis/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ In the code we take these step to set up a plot with two secondary axes and thre
size_t columnY1=ds->addColumnCalculatedFromColumn(columnX, [](double x) { return x; }, "y1");
size_t columnY2=ds->addColumnCalculatedFromColumn(columnX, [](double x) { return cos(x); }, "y2");
size_t columnY3=ds->addColumnCalculatedFromColumn(columnX, [](double x) { return x*x; }, "y3");
size_t columnY4=ds->addColumnCalculatedFromColumn(columnX, [](double x) { return sqrt(x); }, "y3");
```
3. create a second y-axis and set its formating options, so it only draws an axis on the right
Expand All @@ -31,7 +32,7 @@ In the code we take these step to set up a plot with two secondary axes and thre
plot.getYAxis(yAxisRef2)->setShowZeroAxis(false);
```

... and ctreate third y-axis
... and create third y-axis

```.cpp
auto yAxisRef3=plot.getPlotter()->addSecondaryYAxis(new JKQTPVerticalAxis(plot.getPlotter(), JKQTPPrimaryAxis));
Expand All @@ -42,6 +43,17 @@ In the code we take these step to set up a plot with two secondary axes and thre
plot.getYAxis(yAxisRef3)->setShowZeroAxis(false);
```
... and a fourth y-axis (on the primary side)
```.cpp
auto yAxisRef4=plot.getPlotter()->addSecondaryYAxis(new JKQTPVerticalAxis(plot.getPlotter(), JKQTPPrimaryAxis));
plot.getYAxis(yAxisRef4)->setDrawGrid(false);
plot.getYAxis(yAxisRef4)->setDrawMode1(JKQTPCADMcomplete);
plot.getYAxis(yAxisRef4)->setDrawMode2(JKQTPCADMnone);
plot.getYAxis(yAxisRef4)->setDrawMode0(JKQTPCADMnone);
plot.getYAxis(yAxisRef4)->setShowZeroAxis(false);
```

... reformat the major y-axis, so it does not draw on the right and thus the secondary axis yAxisRef2 replaces it there. If this step is omitted, the secondary axes stack on the right of the primary.

```.cpp
Expand Down Expand Up @@ -98,15 +110,27 @@ set axis label
plot.getYAxis(yAxisRef3)->setColor(graph3->getLineColor());
plot.getYAxis(yAxisRef3)->setAxisLabel("graph3");
graph3->setYAxis(yAxisRef3);
```
3.5 the fourth graph uses the default (primary) x-axis, but the secondary axis yAxisRef4 as y-axis
```.cpp
JKQTPXYLineGraph* graph4=new JKQTPXYLineGraph(&plot);
graph4->setKeyColumn(columnX);
graph4->setValueColumn(columnY4);
plot.addGraph(graph3);
plot.getYAxis(yAxisRef4)->setColor(graph4->getLineColor());
plot.getYAxis(yAxisRef4)->setAxisLabel("graph4");
graph4->setYAxis(yAxisRef4);
```

4. autoscale the plot so the graph is contained. This auto-scales all axes using the graphs (and their data) as assigned to the axes, i.e.:
- all 3 graphs for x-axis,
- graph1 for primary y-axis,
- graph2 for secondary axis yAxisRef2
- graph3 for secondary axis yAxisRef3
- all 3 graphs for x-axis,
- graph1 for primary y-axis,
- graph2 for secondary axis yAxisRef2
- graph3 for secondary axis yAxisRef3
- graph4 for secondary axis yAxisRef4
.

```.cpp
plot.zoomToFit();
Expand Down
36 changes: 35 additions & 1 deletion examples/second_axis/second_axis.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ void exampleSecondYAxis(JKQTPlotter& plot, const QString& title)
size_t columnY1=ds->addColumnCalculatedFromColumn(columnX, [](double x) { return x; }, "y1");
size_t columnY2=ds->addColumnCalculatedFromColumn(columnX, [](double x) { return cos(x); }, "y2");
size_t columnY3=ds->addColumnCalculatedFromColumn(columnX, [](double x) { return x*x; }, "y3");
size_t columnY4=ds->addColumnCalculatedFromColumn(columnX, [](double x) { return sqrt(x); }, "y3");

// 3. create a second y-axis and set its formating options, so it only draws an axis on the right
auto yAxisRef2=plot.getPlotter()->addSecondaryYAxis(new JKQTPVerticalAxis(plot.getPlotter(), JKQTPPrimaryAxis));
Expand All @@ -30,13 +31,20 @@ void exampleSecondYAxis(JKQTPlotter& plot, const QString& title)
plot.getYAxis(yAxisRef2)->setDrawMode2(JKQTPCADMcomplete);
plot.getYAxis(yAxisRef2)->setDrawMode0(JKQTPCADMnone);
plot.getYAxis(yAxisRef2)->setShowZeroAxis(false);
// ... and ctreate third y-axis
// ... and create third y-axis
auto yAxisRef3=plot.getPlotter()->addSecondaryYAxis(new JKQTPVerticalAxis(plot.getPlotter(), JKQTPPrimaryAxis));
plot.getYAxis(yAxisRef3)->setDrawGrid(false);
plot.getYAxis(yAxisRef3)->setDrawMode1(JKQTPCADMnone);
plot.getYAxis(yAxisRef3)->setDrawMode2(JKQTPCADMcomplete);
plot.getYAxis(yAxisRef3)->setDrawMode0(JKQTPCADMnone);
plot.getYAxis(yAxisRef3)->setShowZeroAxis(false);
// ... and create fourth y-axis
auto yAxisRef4=plot.getPlotter()->addSecondaryYAxis(new JKQTPVerticalAxis(plot.getPlotter(), JKQTPPrimaryAxis));
plot.getYAxis(yAxisRef4)->setDrawGrid(false);
plot.getYAxis(yAxisRef4)->setDrawMode1(JKQTPCADMcomplete);
plot.getYAxis(yAxisRef4)->setDrawMode2(JKQTPCADMnone);
plot.getYAxis(yAxisRef4)->setDrawMode0(JKQTPCADMnone);
plot.getYAxis(yAxisRef4)->setShowZeroAxis(false);
// ... reformat the major y-axis, so it does not draw on the right and thus the secondary axis yAxisRef2 replaces it there
// if this step is omitted, the secondary axes stack on the right of the primary.
plot.getYAxis()->setDrawMode2(JKQTPCADMnone);
Expand Down Expand Up @@ -72,6 +80,15 @@ void exampleSecondYAxis(JKQTPlotter& plot, const QString& title)
plot.getYAxis(yAxisRef3)->setAxisLabel("graph3");
graph3->setYAxis(yAxisRef3);

// 3.4 the fourth graph uses the default (primary) x-axis, but the ternary axis yAxisRef4 as y-axis
JKQTPXYLineGraph* graph4=new JKQTPXYLineGraph(&plot);
graph4->setKeyColumn(columnX);
graph4->setValueColumn(columnY4);
plot.addGraph(graph3);
plot.getYAxis(yAxisRef4)->setColor(graph4->getLineColor());
plot.getYAxis(yAxisRef4)->setAxisLabel("graph4");
graph4->setYAxis(yAxisRef4);


// 4. autoscale the plot so the graph is contained
// This auto-scales all axes using the graphs (and their data) as assigned to the axes!
Expand Down Expand Up @@ -99,6 +116,7 @@ void exampleSecondXAxis(JKQTPlotter& plot, const QString& title)
size_t columnX1=ds->addColumnCalculatedFromColumn(columnY, [](double x) { return x; }, "x1");
size_t columnX2=ds->addColumnCalculatedFromColumn(columnY, [](double x) { return cos(x); }, "x2");
size_t columnX3=ds->addColumnCalculatedFromColumn(columnY, [](double x) { return x*x; }, "x3");
size_t columnX4=ds->addColumnCalculatedFromColumn(columnY, [](double x) { return sqrt(x); }, "x4");

JKQTPXYLineGraph* graph1=new JKQTPXYLineGraph(&plot);
graph1->setXColumn(columnX1);
Expand All @@ -112,6 +130,10 @@ void exampleSecondXAxis(JKQTPlotter& plot, const QString& title)
graph3->setXColumn(columnX3);
graph3->setYColumn(columnY);
plot.addGraph(graph3);
JKQTPXYLineGraph* graph4=new JKQTPXYLineGraph(&plot);
graph4->setXColumn(columnX4);
graph4->setYColumn(columnY);
plot.addGraph(graph4);

plot.getXAxis()->setDrawMode2(JKQTPCADMLineTicksTickLabels);
plot.getXAxis()->setColor(graph1->getLineColor());
Expand All @@ -137,6 +159,16 @@ void exampleSecondXAxis(JKQTPlotter& plot, const QString& title)
plot.getXAxis(xAxisRef3)->setAxisLabel("graph3");
graph3->setXAxis(xAxisRef3);

auto xAxisRef4=plot.getPlotter()->addSecondaryXAxis(new JKQTPHorizontalAxis(plot.getPlotter(), JKQTPPrimaryAxis));
plot.getXAxis(xAxisRef4)->setDrawGrid(false);
plot.getXAxis(xAxisRef4)->setDrawMode1(JKQTPCADMcomplete);
plot.getXAxis(xAxisRef4)->setDrawMode2(JKQTPCADMnone);
plot.getXAxis(xAxisRef4)->setDrawMode0(JKQTPCADMnone);
plot.getXAxis(xAxisRef4)->setShowZeroAxis(false);
plot.getXAxis(xAxisRef4)->setColor(graph4->getLineColor());
plot.getXAxis(xAxisRef4)->setAxisLabel("graph4");
graph4->setXAxis(xAxisRef4);

plot.zoomToFit();

plot.setWindowTitle(title);
Expand All @@ -152,8 +184,10 @@ int main(int argc, char* argv[])


JKQTPlotter plotsecondY;
//plotsecondY.getPlotter()->enableDebugShowRegionBoxes(true);
exampleSecondYAxis(plotsecondY, "1: Second Y-Axis");
JKQTPlotter plotsecondX;
//plotsecondX.getPlotter()->enableDebugShowRegionBoxes(true);
exampleSecondXAxis(plotsecondX, "2: Second X-Axis");

return app.exec();
Expand Down
55 changes: 36 additions & 19 deletions lib/jkqtplotter/jkqtpbaseplotter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -919,12 +919,16 @@ void JKQTBasePlotter::calcPlotScaling(JKQTPEnhancedPainter& painter){

// read size required by secondary axes
for (const auto& ax: qAsConst(secondaryYAxis)) {
internalPlotMargins[PlotMarginUse::muAxesOutside].left+=ax->getSize1(painter).requiredSize+plotterStyle.secondaryAxisSeparation;
internalPlotMargins[PlotMarginUse::muAxesOutside].right+=ax->getSize2(painter).requiredSize+plotterStyle.secondaryAxisSeparation;
const auto s1=ax->getSize1(painter);
const auto s2=ax->getSize2(painter);
internalPlotMargins[PlotMarginUse::muAxesOutside].left+=s1.requiredSize+((fabs(s1.requiredSize)>0.1)?pt2px(painter, plotterStyle.secondaryAxisSeparation):0.0);
internalPlotMargins[PlotMarginUse::muAxesOutside].right+=s2.requiredSize+((fabs(s2.requiredSize)>0.1)?pt2px(painter, plotterStyle.secondaryAxisSeparation):0.0);
}
for (const auto& ax: qAsConst(secondaryXAxis)) {
internalPlotMargins[PlotMarginUse::muAxesOutside].bottom+=ax->getSize1(painter).requiredSize+plotterStyle.secondaryAxisSeparation;
internalPlotMargins[PlotMarginUse::muAxesOutside].top+=ax->getSize2(painter).requiredSize+plotterStyle.secondaryAxisSeparation;
const auto s1=ax->getSize1(painter);
const auto s2=ax->getSize2(painter);
internalPlotMargins[PlotMarginUse::muAxesOutside].bottom+=s1.requiredSize+((fabs(s1.requiredSize)>0.1)?pt2px(painter, plotterStyle.secondaryAxisSeparation):0.0);
internalPlotMargins[PlotMarginUse::muAxesOutside].top+=s2.requiredSize+((fabs(s2.requiredSize)>0.1)?pt2px(painter, plotterStyle.secondaryAxisSeparation):0.0);
}

if (internalPlotMargins.calcRight()<elongateRight) internalPlotMargins[PlotMarginUse::muAxesOutsideExtend].right=elongateRight-internalPlotMargins.calcRight();
Expand Down Expand Up @@ -1079,16 +1083,18 @@ void JKQTBasePlotter::drawSystemXAxis(JKQTPEnhancedPainter& painter) {

if (secondaryXAxis.size()>0) {
double ibMove1=xAxis->getSize1(painter).requiredSize;
if (ibMove1>0) ibMove1+=plotterStyle.secondaryAxisSeparation;
//if (ibMove1>0) ibMove1+=pt2px(painter, plotterStyle.secondaryAxisSeparation);
double ibMove2=xAxis->getSize2(painter).requiredSize;
if (ibMove2>0) ibMove1+=plotterStyle.secondaryAxisSeparation;
//if (ibMove2>0) ibMove2+=pt2px(painter, plotterStyle.secondaryAxisSeparation);

for (auto& ax: secondaryXAxis) {
ax->drawAxes(painter, ibMove1, ibMove2);
const double add1=ax->getSize1(painter).requiredSize;
const double add2=ax->getSize2(painter).requiredSize;
if (add1>0) ibMove1+=add1+plotterStyle.secondaryAxisSeparation;
if (add2>0) ibMove2+=add2+plotterStyle.secondaryAxisSeparation;
if (add1>0) ibMove1+=pt2px(painter, plotterStyle.secondaryAxisSeparation);
if (add2>0) ibMove2+=pt2px(painter, plotterStyle.secondaryAxisSeparation);
ax->drawAxes(painter, ibMove1, ibMove2);
ibMove1+=add1;
ibMove2+=add2;
}
}
}
Expand All @@ -1103,16 +1109,18 @@ void JKQTBasePlotter::drawSystemYAxis(JKQTPEnhancedPainter& painter) {

if (secondaryYAxis.size()>0) {
double ibMove1=yAxis->getSize1(painter).requiredSize;
if (ibMove1>0) ibMove1+=plotterStyle.secondaryAxisSeparation;
//if (ibMove1>0) ibMove1+=pt2px(painter, plotterStyle.secondaryAxisSeparation);
double ibMove2=yAxis->getSize2(painter).requiredSize;
if (ibMove2>0) ibMove1+=plotterStyle.secondaryAxisSeparation;
//if (ibMove2>0) ibMove2+=pt2px(painter, plotterStyle.secondaryAxisSeparation);

for (auto& ax: secondaryYAxis) {
ax->drawAxes(painter, ibMove1, ibMove2);
const double add1=ax->getSize1(painter).requiredSize;
const double add2=ax->getSize2(painter).requiredSize;
if (add1>0) ibMove1+=add1+plotterStyle.secondaryAxisSeparation;
if (add2>0) ibMove2+=add2+plotterStyle.secondaryAxisSeparation;
if (add1>0) ibMove1+=pt2px(painter, plotterStyle.secondaryAxisSeparation);
if (add2>0) ibMove2+=pt2px(painter, plotterStyle.secondaryAxisSeparation);
ax->drawAxes(painter, ibMove1, ibMove2);
ibMove1+=add1;
ibMove2+=add2;
}
}
}
Expand Down Expand Up @@ -3064,11 +3072,20 @@ QMap<JKQTPCoordinateAxisRef, JKQTPVerticalAxisBase *> JKQTBasePlotter::getYAxes(

QList<JKQTPCoordinateAxis *> JKQTBasePlotter::getAxes(bool includePrimaries)
{
QList<JKQTPCoordinateAxis *> res;
if (includePrimaries) res<<xAxis<<yAxis;
for (auto& ax: secondaryXAxis) res<<ax;
for (auto& ax: secondaryYAxis) res<<ax;
return res;
QList<JKQTPCoordinateAxis *> res;
if (includePrimaries) res<<xAxis<<yAxis;
for (auto& ax: secondaryXAxis) res<<ax;
for (auto& ax: secondaryYAxis) res<<ax;
return res;
}

QList<const JKQTPCoordinateAxis *> JKQTBasePlotter::getAxes(bool includePrimaries) const
{
QList<const JKQTPCoordinateAxis *> res;
if (includePrimaries) res<<xAxis<<yAxis;
for (auto& ax: secondaryXAxis) res<<ax;
for (auto& ax: secondaryYAxis) res<<ax;
return res;
}

JKQTPCoordinateAxisRef JKQTBasePlotter::addSecondaryXAxis(JKQTPHorizontalAxisBase *axis)
Expand Down
2 changes: 2 additions & 0 deletions lib/jkqtplotter/jkqtpbaseplotter.h
Original file line number Diff line number Diff line change
Expand Up @@ -1201,6 +1201,8 @@ class JKQTPLOTTER_LIB_EXPORT JKQTBasePlotter: public QObject {
QMap<JKQTPCoordinateAxisRef, JKQTPVerticalAxisBase*> getYAxes(bool includePrimary=true);
/** \brief returns all available x- or y-axes */
QList<JKQTPCoordinateAxis*> getAxes(bool includePrimaries=true);
/** \brief returns all available x- or y-axes */
QList<const JKQTPCoordinateAxis*> getAxes(bool includePrimaries=true) const;

/** \brief adds a secondary x-axis
*
Expand Down
Loading

0 comments on commit 4cd8a46

Please sign in to comment.