Replies: 7 comments 24 replies
-
几个地方有点小错误:
另外有个问题是,如果用户手动指定的数据类型是错误的,会咋样呢。 |
Beta Was this translation helpful? Give feedback.
-
@ixuxinyue 需要考虑 dataType 和 AVA LOM 的兼容性 |
Beta Was this translation helpful? Give feedback.
-
要考虑两个点的平衡:
这个点建议暂时 hold,先做 3.21 相关紧急重要内容: |
Beta Was this translation helpful? Give feedback.
-
如果这里觉得 dataType 这个配置比较丑的话(大部分人都可能认为),可以改成 measure,因为 vega-lite 的 type 其实也是基于这个来的。 使用方式就如下了: G2.render({
type: 'interval',
data: [
{ genre: 'Sports', sold: 275 },
{ genre: 'Strategy', sold: 115 },
{ genre: 'Action', sold: 120 },
{ genre: 'Shooter', sold: 350 },
{ genre: 'Other', sold: 150 },
],
encode: {
x: { measure: 'categorical', value: 'genre' }, // 显式声明为离散的数据
y: { measure: 'quantitative', value: 'sold' }, // 显式声明为连续的数据
},
}); |
Beta Was this translation helpful? Give feedback.
-
这里不能作为 data def 的原因是:同一个字段可能帮绑定多个通道,而这些通道的类型是不同的,比如如下的例子中:sold 字段既和连续的 x 通道绑定,又和离散的颜色通道绑定。 |
Beta Was this translation helpful? Give feedback.
-
@neoddish @ixuxinyue 应该是不阻断,用 scale.type 就能解决吧。 function getScaleType(type) {
if (type === 'categorical') return 'ordinal';
if (type === 'quantitative') return 'linear';
if (type === 'temporal') return 'time';
} 当前在 encode 中增加 type 会导致 encode api 负责度高很多,倒是可以在 data.fieldDef 中设置,但是如果不阻断的话,也可以暂时不加。 |
Beta Was this translation helpful? Give feedback.
-
type Primitive = number | string | boolean | Date;
type TabularData = Record<string, Primitive>[];
type Callback = (
datum: Record<string, Primitive>,
index: number,
data: TabularData,
) => Primitive;
type Encode = Primitive | Callback;
type ScaleType =
| 'linear'
| 'log'
| 'pow'
| 'sqrt'
| 'qunatile'
| 'threshold'
| 'quantize'
| 'sequential'
| 'time'
| 'point'
| 'band'
| 'ordinal';
type DataType = 'quantitative' | 'categorical' | 'temporal';
function isQuantitative(d: any): boolean {
return typeof d === 'number';
}
function isCategorical(d: any): boolean {
return typeof d === 'string' || typeof d === 'boolean';
}
function isTemporal(d: any): boolean {
return d instanceof Date;
}
function isField(data: TabularData, encode: Encode): encode is string {
return (
typeof encode === 'string' && data.some((d) => d[encode] !== undefined)
);
}
function isTransform(data: TabularData, encode: Encode): encode is Callback {
return typeof encode === 'function';
}
function scaleType2dataType(scaleType: ScaleType): DataType {
switch (scaleType) {
case 'linear':
case 'log':
case 'pow':
case 'sqrt':
case 'qunatile':
case 'threshold':
case 'quantize':
case 'sequential':
return 'quantitative';
case 'time':
return 'temporal';
case 'ordinal':
case 'point':
case 'band':
return 'categorical';
}
}
function values2dataType(values: Primitive[]): DataType {
if (values.some(isQuantitative)) return 'quantitative';
if (values.some(isCategorical)) return 'categorical';
if (values.some(isTemporal)) return 'temporal';
throw new Error(`Unknown type: ${typeof values[0]}`);
}
function columnOf(data: TabularData, encode: Encode): Primitive[] {
if (isTransform(data, encode)) return data.map(encode);
if (isField(data, encode)) return data.map((d) => d[encode]);
return data.map((d) => encode);
}
function inferDataType(
encode: Encode,
scale: ScaleType,
data: TabularData,
): DataType {
if (scale !== undefined) return scaleType2dataType(scale);
const values = columnOf(data, encode);
return values2dataType(values);
} |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
channel.dataType
希望给 G2 支持如下的写法:
支持的 dataType 类型如下:
['a' , 'b', 'c']
['red', 'blue', yellow']
[1, 2, 3]
大部分用户应该感知不到这个特性,所以不会增加他们的学习成本。
存在问题
目前 data 的类型会在创建 Scale 的进行推断,并且反应在对应的比例尺上,比如 ordinal 数据的使用 ordinal 比例尺。但是这样会存在一些问题:
sortX
transform 针对离散和连续数据的排序方法是不同的,所以就必须显式的声明isOrdinal
配置。但是后面又会推断一次,如果推断的不一样会略显奇怪。Ordinal
Categorical
Temporal
SortX
Beta Was this translation helpful? Give feedback.
All reactions