Skip to content

Commit

Permalink
Merge pull request #454 from vizzuhq/autorotate
Browse files Browse the repository at this point in the history
Autorotate xAxisLabel to 45˚
  • Loading branch information
schaumb authored Jan 15, 2024
2 parents 2dd7d41 + af0f1a9 commit 4feaa9a
Show file tree
Hide file tree
Showing 42 changed files with 312 additions and 140 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
- Remove unused marker selection and selected marker coloring.
- Remove cursor modification over logo.
- Make `channel.step` option to work on dimensions.
- When X axis dimension labels are close to each other, they are rotated to avoid overlapping.

## [0.9.3] - 2023-12-20

Expand Down
49 changes: 27 additions & 22 deletions src/apps/qutils/canvas.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -166,25 +166,7 @@ void BaseCanvas::setClipPolygon()

void BaseCanvas::setFont(const Gfx::Font &newFont)
{
auto font = painter.font();
font.setPixelSize(static_cast<int>(newFont.size));

if (!newFont.family.empty())
font.setFamily(QString::fromStdString(newFont.family));

font.setWeight(newFont.weight == Gfx::Font::Weight::Bold()
? QFont::Bold
: newFont.weight == Gfx::Font::Weight::Normal()
? QFont::Normal
: static_cast<int>(newFont.weight) / 10);

font.setStyle(newFont.style == Gfx::Font::Style::italic
? QFont::StyleItalic
: newFont.style == Gfx::Font::Style::oblique
? QFont::StyleOblique
: QFont::StyleNormal);

painter.setFont(font);
painter.setFont(fromGfxFont(newFont, painter.font()));
}

void BaseCanvas::setTextColor(const Gfx::Color &color)
Expand Down Expand Up @@ -253,10 +235,12 @@ QPen BaseCanvas::brushToPen(const QBrush &brush)
return pen;
}

Geom::Size BaseCanvas::textBoundary(const std::string &text)
Geom::Size Gfx::ICanvas::textBoundary(const Gfx::Font &font,
const std::string &text)
{
auto res =
QFontMetrics{painter.font()}.boundingRect(QRect(0, 0, 0, 0),
QFontMetrics{BaseCanvas::fromGfxFont(font)}.boundingRect(
QRect(0, 0, 0, 0),
Qt::AlignLeft,
QString::fromStdString(text));

Expand All @@ -280,4 +264,25 @@ void BaseCanvas::transform(const Geom::AffineTransform &transform)

void BaseCanvas::save() { painter.save(); }

void BaseCanvas::restore() { painter.restore(); }
void BaseCanvas::restore() { painter.restore(); }

QFont BaseCanvas::fromGfxFont(const Gfx::Font &newFont, QFont font)
{
font.setPixelSize(static_cast<int>(newFont.size));

if (!newFont.family.empty())
font.setFamily(QString::fromStdString(newFont.family));

font.setWeight(newFont.weight == Gfx::Font::Weight::Bold()
? QFont::Bold
: newFont.weight == Gfx::Font::Weight::Normal()
? QFont::Normal
: static_cast<int>(newFont.weight) / 10);

font.setStyle(newFont.style == Gfx::Font::Style::italic
? QFont::StyleItalic
: newFont.style == Gfx::Font::Style::oblique
? QFont::StyleOblique
: QFont::StyleNormal);
return font;
}
5 changes: 3 additions & 2 deletions src/apps/qutils/canvas.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@ class BaseCanvas : public Gfx::ICanvas, public Vizzu::Draw::Painter
~BaseCanvas() override;
void init(QPaintDevice *device);

Geom::Size textBoundary(const std::string &text) override;

Gfx::ICanvas &getCanvas() override { return *this; }

[[nodiscard]] Geom::Rect getClipRect() const override;
Expand Down Expand Up @@ -65,6 +63,9 @@ class BaseCanvas : public Gfx::ICanvas, public Vizzu::Draw::Painter
return static_cast<Vizzu::Draw::Painter *>(this);
}

[[nodiscard]] static QFont fromGfxFont(const Gfx::Font &newFont,
QFont font = {});

protected:
QPainter painter;
QFont font;
Expand Down
1 change: 0 additions & 1 deletion src/apps/weblib/canvas.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ Canvas:
rectangle: { x: number, y: number, sizex: number, sizey: number }
circle: { x: number, y: number, radius: number }
line: { x1: number, y1: number, x2: number, y2: number }
textBoundary: { text: C.CString, sizeX: C.CPointer/double *, sizeY: C.CPointer/double * }
text: { x: number, y: number, sizex: number, sizey: number, text: C.CString }
setBrushGradient:
{
Expand Down
10 changes: 10 additions & 0 deletions src/apps/weblib/interface.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
mergeInto(LibraryManager.library, {
openUrl: function (url) {
window.open(UTF8ToString(url), '_blank')
},
textBoundary: function (font, text, sizeX, sizeY) {
const dc = Module.measureCanvas
dc.font = UTF8ToString(font)
let metrics = dc.measureText(UTF8ToString(text))
const width = metrics.width
metrics = dc.measureText('Op')
const height = metrics.actualBoundingBoxAscent + metrics.actualBoundingBoxDescent
setValue(sizeX, width, 'double')
setValue(sizeY, height, 'double')
}
})
2 changes: 2 additions & 0 deletions src/apps/weblib/interfacejs.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@

extern "C" {
extern void openUrl(const char *);
extern void
textBoundary(const char *, const char *, double *, double *);
}

#endif
18 changes: 11 additions & 7 deletions src/apps/weblib/jscriptcanvas.cpp
Original file line number Diff line number Diff line change
@@ -1,17 +1,11 @@
#include "jscriptcanvas.h"

#include "canvas.h"
#include "interfacejs.h"

namespace Vizzu::Main
{

Geom::Size JScriptCanvas::textBoundary(const std::string &text)
{
Geom::Size res;
::canvas_textBoundary(this, text.c_str(), &res.x, &res.y);
return res;
}

Geom::Rect JScriptCanvas::getClipRect() const
{
return clipRect ? *clipRect : Geom::Rect::CenteredMax();
Expand Down Expand Up @@ -244,4 +238,14 @@ void JScriptCanvas::resetStates()
clipRect = std::nullopt;
}

}

Geom::Size Gfx::ICanvas::textBoundary(const Gfx::Font &font,
const std::string &text)
{
thread_local std::string fontCache;
fontCache = font.toCSS();
Geom::Size res;
::textBoundary(fontCache.c_str(), text.c_str(), &res.x, &res.y);
return res;
}
2 changes: 0 additions & 2 deletions src/apps/weblib/jscriptcanvas.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@ class JScriptCanvas : public Gfx::ICanvas, public Draw::Painter
JScriptCanvas() = default;
~JScriptCanvas() override = default;

Geom::Size textBoundary(const std::string &text) override;

[[nodiscard]] Geom::Rect getClipRect() const override;
void setClipRect(const Geom::Rect &rect) override;
void setClipCircle(const Geom::Circle &circle) override;
Expand Down
1 change: 1 addition & 0 deletions src/apps/weblib/ts-api/cvizzu.types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ export interface ModuleOptions {
export interface CVizzu {
// decorations
canvases: { [key: CPointer]: Canvas }
measureCanvas: CanvasRenderingContext2D

// members
HEAPU8: Uint8Array
Expand Down
6 changes: 1 addition & 5 deletions src/apps/weblib/ts-api/module/ccanvas.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { CEnv, CObject } from './cenv.js'
import { CPointerClosure } from './objregistry.js'
import { CColorGradient } from './ccolorgradient.js'
import { CString, CPointer, CColorGradientPtr } from '../cvizzu.types'
import { CString, CColorGradientPtr } from '../cvizzu.types'

export class CCanvas extends CObject {
constructor(env: CEnv, getId: CPointerClosure) {
Expand All @@ -15,8 +15,4 @@ export class CCanvas extends CObject {
getString(text: CString): string {
return this._wasm.UTF8ToString(text)
}

setNumber(cNumber: CPointer, value: number): void {
this._wasm.setValue(cNumber, value, 'double')
}
}
3 changes: 3 additions & 0 deletions src/apps/weblib/ts-api/module/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ import { Canvas } from './canvas'
export class Module extends CEnv {
constructor(wasm: CVizzu) {
super(wasm, new ObjectRegistry(wasm._object_free))
const context2D = (<HTMLCanvasElement>document.createElement('canvas')).getContext('2d')
if (!context2D) throw new Error('Failed to get 2D context')
this._wasm.measureCanvas = context2D
this._wasm.canvases = {}
this.setLogging(false)
}
Expand Down
12 changes: 1 addition & 11 deletions src/apps/weblib/ts-api/render.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { CPointer, CString, CColorGradientPtr } from './cvizzu.types'
import { CString, CColorGradientPtr } from './cvizzu.types'
import { Plugin, PluginApi } from './plugins.js'
import { Canvas } from './module/canvas.js'
import { Module } from './module/module.js'
Expand Down Expand Up @@ -179,16 +179,6 @@ export class Render implements Plugin, Canvas {
if (this._currentLineWidth !== 0) dc.stroke()
}

textBoundary(text: CString, sizeX: CPointer, sizeY: CPointer): void {
const dc = this._canvas.context
let metrics = dc.measureText(this._ccanvas.getString(text))
const width = metrics.width
metrics = dc.measureText('Op')
const height = metrics.actualBoundingBoxAscent + metrics.actualBoundingBoxDescent
this._ccanvas.setNumber(sizeX, width)
this._ccanvas.setNumber(sizeY, height)
}

text(x: number, y: number, sizex: number, sizey: number, text: CString): void {
const dc = this._canvas.context
dc.textAlign = 'left'
Expand Down
4 changes: 3 additions & 1 deletion src/base/gfx/canvas.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ struct ICanvas
{
virtual ~ICanvas() = default;

virtual Geom::Size textBoundary(const std::string &string) = 0;
[[nodiscard]] virtual Geom::Rect getClipRect() const = 0;
virtual void setClipRect(const Geom::Rect &rect) = 0;
virtual void setClipCircle(const Geom::Circle &circle) = 0;
Expand Down Expand Up @@ -65,6 +64,9 @@ struct ICanvas
virtual void frameEnd() = 0;

virtual void *getPainter() = 0;

static Geom::Size textBoundary(const Gfx::Font &,
const std::string &);
};

}
Expand Down
3 changes: 2 additions & 1 deletion src/base/gfx/draw/textbox.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,8 @@ Geom::Size TextBox::measure(ICanvas &canvas)
line.height = 0;
for (auto &text : line.texts) {
canvas.setFont(text.font);
auto size = canvas.textBoundary(text.content);
auto size =
ICanvas::textBoundary(text.font, text.content);
text.width = size.x;
line.width += size.x;
if (size.y > line.height) line.height = size.y;
Expand Down
5 changes: 5 additions & 0 deletions src/base/math/range.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,11 @@ template <typename T> struct Range
return value >= min && value <= max;
}

[[nodiscard]] bool includes(const Range<T> &range) const
{
return range.max >= min && range.min <= max;
}

[[nodiscard]] T rescale(const T &value) const
{
return max == min ? 0.5 : (value - min) / size();
Expand Down
38 changes: 1 addition & 37 deletions src/base/style/sheet.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,20 +32,6 @@ template <class Params> class Sheet
return Style::ParamRegistry<Params>::instance().listParams();
}

void setParamDefault(const std::string &path,
const std::string &value)
{
setParam(defaultParams, path, value);
}

void setParam(const std::string &path, const std::string &value)
{
if (!activeParams)
throw std::logic_error("no active parameters set");

setParam(*activeParams, path, value);
}

void setParams(const std::string &path, const std::string &value)
{
if (!activeParams)
Expand All @@ -54,28 +40,6 @@ template <class Params> class Sheet
setParams(*activeParams, path, value);
}

static bool hasParam(const std::string &path)
{
return Style::ParamRegistry<Params>::instance().hasParam(
path);
}

static void setParam(Params &params,
const std::string &path,
const std::string &value)
{
if (!hasParam(path))
throw std::logic_error(
path + "/" + value
+ ": non-existent style parameter");

Style::ParamRegistry<Params>::instance().visit(path,
[&](auto &p)
{
p.fromString(params, value);
});
}

static void setParams(Params &params,
const std::string &path,
const std::string &value)
Expand All @@ -99,7 +63,7 @@ template <class Params> class Sheet
+ ": non-existent style parameter");
}

static std::string getParam(Params &params,
[[nodiscard]] static std::string getParam(const Params &params,
const std::string &path)
{
auto &paramReg = Style::ParamRegistry<Params>::instance();
Expand Down
3 changes: 1 addition & 2 deletions src/chart/animator/animator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,7 @@ void Animator::animate(const ::Anim::Control::Option &options,

running = true;
stripActAnimation();
actAnimation = nextAnimation;
nextAnimation = AnimationPtr();
actAnimation = std::exchange(nextAnimation, {});
setupActAnimation();
actAnimation->animate(options, completionCallback);
}
Expand Down
9 changes: 0 additions & 9 deletions src/chart/generator/channelstats.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,6 @@ void ChannelStats::track(double value)
range.include(value);
}

void ChannelStats::trackSingle(double value)
{
if (isDimension)
throw std::logic_error(
"internal error: invalid dimension channel tracking");

sum += value;
}

void ChannelStats::track(const Marker::Id &id)
{
if (isDimension)
Expand Down
2 changes: 0 additions & 2 deletions src/chart/generator/channelstats.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,12 @@ class ChannelStats
public:
bool isDimension;
Math::Range<double> range;
double sum{0.0};
std::vector<Data::MultiDim::SubSliceIndex> usedIndices;

ChannelStats() : isDimension(true) {}
ChannelStats(const Channel &channel, const Data::DataCube &cube);

void track(double value);
void trackSingle(double value);
void track(const Marker::Id &id);
};

Expand Down
5 changes: 1 addition & 4 deletions src/chart/generator/marker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -220,10 +220,7 @@ double Marker::getValueForChannel(const Channels &channels,
else
value = singlevalue;

if (enabled) {
if (measure) stat.trackSingle(singlevalue);
stat.track(value);
}
if (enabled) { stat.track(value); }
}
return value;
}
Expand Down
Loading

0 comments on commit 4feaa9a

Please sign in to comment.