Tackwin
Tackwin

Reputation: 79

Restrict the scrolling on QChart with QBarCategoryAxis

I'm trying to make the standard Qt barchart example scrollable and zoomable. I can use the scroll and zoom method of QChart to define a custom Chart View class that re-implement the different mouse event. However, with this technique it's possible to scroll past the range of categories.

out of bounds

It'll be a simple fix if i could get the min max value on the y axis in "chart space" but i guess since the y axis is a QBarCategoryAxis the only methods available return the string of the min max category.

I'm at a loss every search i come up with return results for the QValueAxis api.

Upvotes: 1

Views: 353

Answers (1)

Jamie
Jamie

Reputation: 41

I had the same problem for a while. You can derive from QBarCategoryAxis to check boundaries whenever a zoom/scroll/resize event is called.

#include "CustomValueAxis.h"

CustomValueAxis::CustomValueAxis() : lockMin(0), lockMax(0) {}

CustomValueAxis::CustomValueAxis(int min, int max) : lockMin(min), lockMax(max) {}

void CustomValueAxis::preventOutOfRange()
{
    setMin((min() < lockMin) ? lockMin : min());
    setMax((max() > lockMax) ? lockMax : max());
}
#pragma once
#include <QValueAxis>

class CustomValueAxis : public QValueAxis
{
    Q_OBJECT
public:
    int lockMin;
    int lockMax;

    CustomValueAxis();
    CustomValueAxis(int min, int max);

private slots:
    void preventOutOfRange();
};

Then connect a signal to the slot. I did it for QValueAxis but it should be the same for categoryAxis.

Upvotes: 0

Related Questions