Skip to content

Commit b1307f1

Browse files
authored
Merge pull request #627 from vizzuhq/add-dimension-contiguous
Add isContiguous to dimension
2 parents d0cdcf2 + 1bd106d commit b1307f1

File tree

21 files changed

+223
-235
lines changed

21 files changed

+223
-235
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
- Move split align sort and reverse to AxisChannel
2525
- Add new sorting strategy: 'byLabel'.
2626
- Enable split and align on mainAxis.
27+
- Add 'isContiguous' property for dimension series
2728

2829
## [0.16.0] - 2024-11-28
2930

src/apps/weblib/cinterface.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -298,14 +298,16 @@ void data_addDimension(APIHandles::Chart chart,
298298
const char **categories,
299299
std::uint32_t categoriesCount,
300300
const std::uint32_t *categoryIndices,
301-
std::uint32_t categoryIndicesCount)
301+
std::uint32_t categoryIndicesCount,
302+
bool isContiguous)
302303
{
303304
Interface::getInstance().addDimension(chart,
304305
name,
305306
categories,
306307
categoriesCount,
307308
categoryIndices,
308-
categoryIndicesCount);
309+
categoryIndicesCount,
310+
isContiguous);
309311
}
310312

311313
void data_addMeasure(APIHandles::Chart chart,

src/apps/weblib/cinterface.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,8 @@ extern void data_addDimension(APIHandles::Chart chart,
8282
const char **categories,
8383
std::uint32_t categoriesCount,
8484
const std::uint32_t *categoryIndices,
85-
std::uint32_t categoryIndicesCount);
85+
std::uint32_t categoryIndicesCount,
86+
bool isContiguous);
8687
extern void data_addMeasure(APIHandles::Chart chart,
8788
const char *name,
8889
const char *unit,

src/apps/weblib/interface.cpp

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -319,13 +319,14 @@ void Interface::addDimension(ObjectRegistryHandle chart,
319319
const char **categories,
320320
std::uint32_t categoriesCount,
321321
const std::uint32_t *categoryIndices,
322-
std::uint32_t categoryIndicesCount)
322+
std::uint32_t categoryIndicesCount,
323+
bool isContiguous)
323324
{
324-
if (categories) {
325-
getChart(chart)->getTable().addColumn(name,
326-
{categories, categoriesCount},
327-
{categoryIndices, categoryIndicesCount});
328-
}
325+
getChart(chart)->getTable().add_dimension(
326+
{categories, categoriesCount},
327+
{categoryIndices, categoryIndicesCount},
328+
name,
329+
{{{"isContiguous", isContiguous ? "true" : "false"}}});
329330
}
330331

331332
void Interface::addMeasure(ObjectRegistryHandle chart,
@@ -334,22 +335,22 @@ void Interface::addMeasure(ObjectRegistryHandle chart,
334335
const double *values,
335336
std::uint32_t count)
336337
{
337-
getChart(chart)->getTable().addColumn(name,
338-
unit,
339-
{values, count});
338+
getChart(chart)->getTable().add_measure({values, count},
339+
name,
340+
{{std::pair{"unit", unit}}});
340341
}
341342

342343
void Interface::addRecord(ObjectRegistryHandle chart,
343344
const char *const *cells,
344345
std::uint32_t count)
345346
{
346-
getChart(chart)->getTable().pushRow({cells, count});
347+
getChart(chart)->getTable().add_record({cells, count});
347348
}
348349

349350
const char *Interface::dataMetaInfo(ObjectRegistryHandle chart)
350351
{
351352
thread_local std::string res;
352-
res = getChart(chart)->getTable().getInfos();
353+
res = getChart(chart)->getTable().as_string();
353354
return res.c_str();
354355
}
355356

src/apps/weblib/interface.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,8 @@ class Interface
100100
const char **categories,
101101
std::uint32_t categoriesCount,
102102
const std::uint32_t *categoryIndices,
103-
std::uint32_t categoryIndicesCount);
103+
std::uint32_t categoryIndicesCount,
104+
bool isContiguous);
104105
void addMeasure(ObjectRegistryHandle chart,
105106
const char *name,
106107
const char *unit,

src/apps/weblib/ts-api/cvizzu.types.d.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,8 @@ export interface CVizzu {
103103
categories: CArrayPtr,
104104
categoriesCount: number,
105105
categoryIndices: CArrayPtr,
106-
categoryIndicesCount: number
106+
categoryIndicesCount: number,
107+
isContiguous: boolean
107108
): void
108109
_data_addMeasure(
109110
chart: CChartPtr,

src/apps/weblib/ts-api/data.ts

Lines changed: 29 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -83,19 +83,25 @@ export class Data {
8383

8484
if (this._isIndexedDimension(series)) {
8585
this._validateIndexedDimension(series)
86-
this._addDimension(series.name, series.values ?? [], series.categories)
86+
this._addDimension(
87+
series.name,
88+
series.values ?? [],
89+
series.categories,
90+
series.isContiguous ?? false
91+
)
8792
} else {
8893
const values = series?.values ?? ([] as DataTypes[])
8994
const seriesType = series?.type ?? this._detectType(values)
9095

91-
if (seriesType === 'dimension') {
96+
if (this._isDetectedDimension(series, seriesType)) {
9297
const { indexes, categories } = this._convertDimension(values)
93-
this._addDimension(series.name, indexes, categories)
98+
this._addDimension(series.name, indexes, categories, series.isContiguous ?? false)
9499
} else if (this._isMeasure(series, seriesType)) {
95100
if (!series.unit) series.unit = ''
96101
this._addMeasure(series.name, series.unit, values)
97102
} else {
98-
throw new Error('invalid series type: ' + series.type)
103+
const seriesBase = series as D.SeriesBase
104+
throw new Error('invalid series type: ' + seriesBase.type)
99105
}
100106
}
101107
}
@@ -107,11 +113,15 @@ export class Data {
107113
return true
108114
}
109115

116+
private _isDetectedDimension(series: D.Series, type: string | null): series is D.Dimension {
117+
return (
118+
type === 'dimension' ||
119+
('isContiguous' in series && typeof series.isContiguous === 'boolean')
120+
)
121+
}
122+
110123
private _isMeasure(series: D.Series, type: string | null): series is D.Measure {
111-
if (type === 'measure' || ('unit' in series && typeof series.unit === 'string')) {
112-
return true
113-
}
114-
return false
124+
return type === 'measure' || ('unit' in series && typeof series.unit === 'string')
115125
}
116126

117127
private _validateIndexedDimension(series: D.IndexDimension): void {
@@ -151,7 +161,12 @@ export class Data {
151161
return null
152162
}
153163

154-
private _addDimension(name: string, indexes: number[], categories: string[]): void {
164+
private _addDimension(
165+
name: string,
166+
indexes: number[],
167+
categories: string[],
168+
isContiguous: boolean
169+
): void {
155170
if (typeof name !== 'string') {
156171
throw new Error('first parameter should be string')
157172
}
@@ -164,6 +179,10 @@ export class Data {
164179
throw new Error('third parameter should be an array')
165180
}
166181

182+
if (typeof isContiguous !== 'boolean') {
183+
throw new Error('fourth parameter should be boolean')
184+
}
185+
167186
if (!this._isStringArray(categories)) {
168187
throw new Error('third parameter should be an array of strings')
169188
}
@@ -172,7 +191,7 @@ export class Data {
172191
throw new Error('the measure index array element should be number')
173192
}
174193

175-
this._cData.addDimension(name, indexes, categories)
194+
this._cData.addDimension(name, indexes, categories, isContiguous)
176195
}
177196

178197
private _isStringArray(values: unknown[]): values is string[] {

src/apps/weblib/ts-api/module/cdata.ts

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,15 @@ export class CData extends CObject {
3535
return { series: JSON.parse(info) }
3636
}
3737

38-
addDimension(name: string, indexes: number[], categories: string[]): void {
38+
addDimension(
39+
name: string,
40+
indexes: number[],
41+
categories: string[],
42+
isContiguous: boolean
43+
): void {
3944
const categoriesPointer = new Uint32Array(categories.length)
4045
for (let i = 0; i < categories.length; i++) {
41-
const categoryPointer = this._toCString(categories[i]!)
42-
categoriesPointer[i] = categoryPointer
46+
categoriesPointer[i] = this._toCString(categories[i]!)
4347
}
4448

4549
const categoriesPointerArrayLen = categories.length * 4
@@ -66,7 +70,8 @@ export class CData extends CObject {
6670
categoriesPtrArr,
6771
categories.length,
6872
indexesArr,
69-
indexes.length
73+
indexes.length,
74+
isContiguous
7075
)
7176
} finally {
7277
this._wasm._free(cname)

src/apps/weblib/typeschema-api/data.yaml

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,11 @@ definitions:
6969
type: array
7070
items:
7171
$ref: DimensionValue
72+
isContiguous:
73+
description: |
74+
If set to true, the dimension handled as a contiguous dimension
75+
like timestamps.
76+
type: boolean
7277

7378
IndexDimension:
7479
$extends: SeriesBase
@@ -97,6 +102,11 @@ definitions:
97102
type: array
98103
items:
99104
type: number
105+
isContiguous:
106+
description: |
107+
If set to true, the dimension handled as a contiguous dimension
108+
like timestamps.
109+
type: boolean
100110
required: [categories]
101111

102112
ImplicitStringDimension:
@@ -238,7 +248,11 @@ definitions:
238248
length:
239249
description: Count of values in the series.
240250
type: number
241-
required: [type, categories, length]
251+
isContiguous:
252+
description: |
253+
The dimension handled as a contiguous dimension like timestamps.
254+
type: boolean
255+
required: [type, categories, length, isContiguous]
242256

243257
MeasureInfo:
244258
$extends: SeriesBase

src/base/conv/auto_json.h

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -336,11 +336,15 @@ struct JSONObj : protected JSONRepeat<'{', '}'>
336336
return std::move(*this);
337337
}
338338

339-
template <class T> JSONObj &&mergeObj(const T &obj) &&
339+
template <bool isStatic = true, class T>
340+
JSONObj &mergeObj(const T &obj)
340341
{
341342
auto pre_size = json.size();
342343

343-
staticObj(obj);
344+
if constexpr (isStatic)
345+
staticObj(obj);
346+
else
347+
dynamicObj(obj);
344348

345349
json.pop_back();
346350

@@ -351,7 +355,7 @@ struct JSONObj : protected JSONRepeat<'{', '}'>
351355
else
352356
was = true;
353357

354-
return std::move(*this);
358+
return *this;
355359
}
356360
};
357361

0 commit comments

Comments
 (0)