amchart访问结构化数据对象内的值

huangapple go评论66阅读模式
英文:

amchart accessing values inside a structured data object

问题

I've translated the code portion for you:

如何向AMchart的JSON对象添加额外数据 <br/>
当我的对象简单时一切都可以解析<br/>
`var data = [ {year: "1930",italy: 4,germany: 5.1,uk: 3}, {year: "1934",italy: 1,germany: 2,uk: 6}, {year: "1938",italy: 2,germany: 3,uk: 1} ];` <br/>

但是当它看起来像这样时就不行了<br/>
`var data = [ {year:"1930", italy:[{aa:20,bb:"21"}], germany:[{aa:30,bb:"44"}], uk:[{aa:40,bb:"77"}] } ];`

这是负责解析的代码<br/>

var series = chart.series.push(
am5xy.LineSeries.new(root, {
  name: name,
  xAxis: xAxis,
  yAxis: yAxis,
  valueYField: field, // << -- issue
  categoryXField: "year",
  tooltip: am5.Tooltip.new(root, {
    pointerOrientation: "horizontal",
    labelText: "[bold]{name}[/]\n{categoryX}: {valueY}"
  })
})
);

我可以使用这个获取正确的值但是当我输入值并替换上面的 `field` 代码就不能正确解析<br/>

if(chart.series._values[0]){
var data1 = chart.series._values[0]._dataItems[0].dataContext;

if(data1[field][0])
  var valY = data1[field][0].aa;
  
//console.log( valY );
}

这是我的完整代码

am5.ready(function() {

var root = am5.Root.new("chartdiv");
root._logo.dispose();

root.setThemes([
  am5themes_Animated.new(root)
]);

var chart = root.container.children.push(
  am5xy.XYChart.new(root, {
    panX: true,
    panY: true,
    wheelX: "panX",
    wheelY: "zoomX",
    layout: root.verticalLayout,
    pinchZoomX:true
  })
);

// 添加光标
var cursor = chart.set("cursor", am5xy.XYCursor.new(root, {
  behavior: "none"
}));
cursor.lineY.set("visible", false);

// 数据
var data = [ {year:"1930", italy:[{aa:20,bb:"21"}], germany:[{aa:30,bb:"44"}], uk:[{aa:40,bb:"77"}] } ];

// 创建轴
var xRenderer = am5xy.AxisRendererX.new(root, {});
xRenderer.grid.template.set("location", 0.5);
xRenderer.labels.template.setAll({
  location: 0.5,
  multiLocation: 0.5
});

var xAxis = chart.xAxes.push(
  am5xy.CategoryAxis.new(root, {
    categoryField: "year",
    renderer: xRenderer,
    tooltip: am5.Tooltip.new(root, {})
  })
);

xAxis.data.setAll(data);

var yAxis = chart.yAxes.push(
  am5xy.ValueAxis.new(root, {
    maxPrecision: 1,
    renderer: am5xy.AxisRendererY.new(root, {
      inversed: false
    })
  })
);

function createSeries(name, field) { 

  if(chart.series._values[0]){
    var data1 = chart.series._values[0]._dataItems[0].dataContext;
    
    if(data1[field][0])
      var valY = data1[field][0].aa;
      
    //console.log( valY );
  }

  var series = chart.series.push(
    am5xy.LineSeries.new(root, {
      name: name,
      xAxis: xAxis,
      yAxis: yAxis,
      valueYField: field, // 替换为: valY 时出现问题
      categoryXField: "year",
      tooltip: am5.Tooltip.new(root, {
        pointerOrientation: "horizontal",
        labelText: "[bold]{name}[/]\n{categoryX}: {valueY}"
      })
    })
  );
  
  series.bullets.push(function() {
    return am5.Bullet.new(root, {
      sprite: am5.Circle.new(root, {
        radius: 5,
        fill: series.get("fill")
      })
    });
  });

  series.set("setStateOnChildren", true);
  series.states.create("hover", {});

  series.mainContainer.set("setStateOnChildren", true);
  series.mainContainer.states.create("hover", {});

  series.strokes.template.states.create("hover", {
    strokeWidth: 4
  });

  series.data.setAll(data);
  series.show(1000);
}


createSeries("Italy", "italy");
createSeries("Germany", "germany");
createSeries("UK", "uk");

var legend = chart.children.push(
  am5.Legend.new(root, {
    centerX: am5.percent(90),
    x: am5.percent(90),
    centerY: am5.percent(105),
    y: am5.percent(105),
  })
);

// 当悬停在图例项上时,使系列更改状态
legend.itemContainers.template.states.create("hover", {});

legend.itemContainers.template.events.on("pointerover", function(e) {
  e.target.dataItem.dataContext.hover();
});
legend.itemContainers.template.events.on("pointerout", function(e) {
  e.target.dataItem.dataContext.unhover();
});

legend.data.setAll(chart.series.values);

chart.appear(1000, 100);

});

请注意,我已经翻译了代码,但其中包含一些HTML转义字符(如 <br/>),你可能需要根据需要进行调整。

英文:

How can I add extra data to the json object in AMchart? <br/>
When my obj is simple everything parses: <br/>
var data = [ {year: &quot;1930&quot;,italy: 4,germany: 5.1,uk: 3}, {year: &quot;1934&quot;,italy: 1,germany: 2,uk: 6}, {year: &quot;1938&quot;,italy: 2,germany: 3,uk: 1} ]; <br/>

But when it looks like this it does not: <br/>
var data = [ {year:&quot;1930&quot;, italy:[{aa:20,bb:&quot;21&quot;}], germany:[{aa:30,bb:&quot;44&quot;}], uk:[{aa:40,bb:&quot;77&quot;}] } ];

This is the code that is responsible for parsing:<br/>

   var series = chart.series.push(
am5xy.LineSeries.new(root, {
name: name,
xAxis: xAxis,
yAxis: yAxis,
valueYField: field, // &lt;&lt; -- issue
categoryXField: &quot;year&quot;,
tooltip: am5.Tooltip.new(root, {
pointerOrientation: &quot;horizontal&quot;,
labelText: &quot;[bold]{name}[/]\n{categoryX}: {valueY}&quot;
})
})
);

I can get the correct values using this but when i enter the values and replace field above the code does not parse correct,<br/>

  if(chart.series._values[0]){
var data1 = chart.series._values[0]._dataItems[0].dataContext;
if(data1[field][0])
var valY = data1[field][0].aa;
//console.log( valY );
}

here is my complete code:

am5.ready(function() {
var root = am5.Root.new(&quot;chartdiv&quot;);
root._logo.dispose();
root.setThemes([
am5themes_Animated.new(root)
]);
var chart = root.container.children.push(
am5xy.XYChart.new(root, {
panX: true,
panY: true,
wheelX: &quot;panX&quot;,
wheelY: &quot;zoomX&quot;,
layout: root.verticalLayout,
pinchZoomX:true
})
);
// Add cursor
// https://www.amcharts.com/docs/v5/charts/xy-chart/cursor/
var cursor = chart.set(&quot;cursor&quot;, am5xy.XYCursor.new(root, {
behavior: &quot;none&quot;
}));
cursor.lineY.set(&quot;visible&quot;, false);
// The data
var data = [    {year:&quot;1930&quot;, italy:[{aa:20,bb:&quot;21&quot;}], germany:[{aa:30,bb:&quot;44&quot;}], uk:[{aa:40,bb:&quot;77&quot;}] } ];
/* var data = [ {year: &quot;1930&quot;,italy: 4,germany: 5.1,uk: 3},
{year: &quot;1934&quot;,italy: 1,germany: 2,uk: 6},
{year: &quot;1938&quot;,italy: 2,germany: 3,uk: 1} ]; */
// Create axes
// https://www.amcharts.com/docs/v5/charts/xy-chart/axes/
var xRenderer = am5xy.AxisRendererX.new(root, {});
xRenderer.grid.template.set(&quot;location&quot;, 0.5);
xRenderer.labels.template.setAll({
location: 0.5,
multiLocation: 0.5
});
var xAxis = chart.xAxes.push(
am5xy.CategoryAxis.new(root, {
categoryField: &quot;year&quot;,
renderer: xRenderer,
tooltip: am5.Tooltip.new(root, {})
})
);
xAxis.data.setAll(data);
var yAxis = chart.yAxes.push(
am5xy.ValueAxis.new(root, {
maxPrecision: 1,
renderer: am5xy.AxisRendererY.new(root, {
inversed: false
})
})
);
function createSeries(name, field) { 
if(chart.series._values[0]){
var data1 = chart.series._values[0]._dataItems[0].dataContext;
if(data1[field][0])
var valY = data1[field][0].aa;
//console.log( valY );
}
var series = chart.series.push(
am5xy.LineSeries.new(root, {
name: name,
xAxis: xAxis,
yAxis: yAxis,
valueYField: field, // issue when replced with: valY
categoryXField: &quot;year&quot;,
tooltip: am5.Tooltip.new(root, {
pointerOrientation: &quot;horizontal&quot;,
labelText: &quot;[bold]{name}[/]\n{categoryX}: {valueY}&quot;
})
})
);
series.bullets.push(function() {
return am5.Bullet.new(root, {
sprite: am5.Circle.new(root, {
radius: 5,
fill: series.get(&quot;fill&quot;)
})
});
});
series.set(&quot;setStateOnChildren&quot;, true);
series.states.create(&quot;hover&quot;, {});
series.mainContainer.set(&quot;setStateOnChildren&quot;, true);
series.mainContainer.states.create(&quot;hover&quot;, {});
series.strokes.template.states.create(&quot;hover&quot;, {
strokeWidth: 4
});
series.data.setAll(data);
series.show(1000);
}
createSeries(&quot;Italy&quot;, &quot;italy&quot;);
createSeries(&quot;Germany&quot;, &quot;germany&quot;);
createSeries(&quot;UK&quot;, &quot;uk&quot;);
var legend = chart.children.push(
am5.Legend.new(root, {
centerX: am5.percent(90),
x: am5.percent(90),
centerY: am5.percent(105),
y: am5.percent(105),
})
);
// Make series change state when legend item is hovered
legend.itemContainers.template.states.create(&quot;hover&quot;, {});
legend.itemContainers.template.events.on(&quot;pointerover&quot;, function(e) {
e.target.dataItem.dataContext.hover();
});
legend.itemContainers.template.events.on(&quot;pointerout&quot;, function(e) {
e.target.dataItem.dataContext.unhover();
});
legend.data.setAll(chart.series.values);
chart.appear(1000, 100);
});

答案1

得分: 2

以下是翻译好的内容:

"amcharts 似乎不提供通过其 数据字段 结构化访问数据子对象的方式。

因此,唯一可用的解决方案是预处理数据;对于您的数据结构,您可以使用类似以下的方法来覆盖两种情况:

series.data.setAll(data
.map(o => Object.fromEntries(Object.entries(o)
.map(([k, v]) => [k, v?.[0]?.aa ?? v]))
)
)

代码片段如下:

var root = am5.Root.new("chartdiv");
root._logo.dispose();
root.setThemes([
am5themes_Animated.new(root)
]);
var chart = root.container.children.push(
am5xy.XYChart.new(root, {
panX: true,
panY: true,
wheelX: "panX",
wheelY: "zoomX",
layout: root.verticalLayout,
pinchZoomX:true
})
);
// 添加光标
// https://www.amcharts.com/docs/v5/charts/xy-chart/cursor/
var cursor = chart.set("cursor", am5xy.XYCursor.new(root, {
behavior: "none"
}));
cursor.lineY.set("visible", false);
// 数据
var data = [{year: "1930", italy: [{aa:20,bb:"21"}], germany: [{aa:30,bb:"44"}], uk: [{aa:40,bb:"77"}] },
{year: "1934", italy: 1, germany: 2, uk: 6}, {year: "1938", italy: 2, germany: 3, uk: 1}];
// 创建轴
// https://www.amcharts.com/docs/v5/charts/xy-chart/axes/
var xRenderer = am5xy.AxisRendererX.new(root, {});
xRenderer.grid.template.set("location", 0.5);
xRenderer.labels.template.setAll({
location: 0.5,
multiLocation: 0.5
});
var xAxis = chart.xAxes.push(
am5xy.CategoryAxis.new(root, {
categoryField: "year",
renderer: xRenderer,
tooltip: am5.Tooltip.new(root, {})
})
);
xAxis.data.setAll(data);
var yAxis = chart.yAxes.push(
am5xy.ValueAxis.new(root, {
maxPrecision: 1,
renderer: am5xy.AxisRendererY.new(root, {
inversed: false
})
})
);
function createSeries(name, field) {
var series = chart.series.push(
am5xy.LineSeries.new(root, {
name: name,
xAxis: xAxis,
yAxis: yAxis,
valueYField: field,
categoryXField: "year",
tooltip: am5.Tooltip.new(root, {
pointerOrientation: "horizontal",
labelText: "[bold]{name}[/]\n{categoryX}: {valueY}"
})
})
);
series.bullets.push(function() {
return am5.Bullet.new(root, {
sprite: am5.Circle.new(root, {
radius: 5,
fill: series.get("fill")
})
});
});
series.set("setStateOnChildren", true);
series.states.create("hover", {});
series.mainContainer.set("setStateOnChildren", true);
series.mainContainer.states.create("hover", {});
series.strokes.template.states.create("hover", {
strokeWidth: 4
});
series.data.setAll(data.map(o => Object.fromEntries(Object.entries(o).map(([k, v]) => [k, v?.[0]?.aa ?? v]))))
series.show(1000);
}
createSeries("Italy", "italy");
createSeries("Germany", "germany");
createSeries("UK", "uk");
var legend = chart.children.push(
am5.Legend.new(root, {
centerX: am5.percent(90),
x: am5.percent(90),
centerY: am5.percent(105),
y: am5.percent(105),
})
);
// 当图例项悬停时使系列更改状态
legend.itemContainers.template.states.create("hover", {});
legend.itemContainers.template.events.on("pointerover", function(e) {
e.target.dataItem.dataContext.hover();
});
legend.itemContainers.template.events.on("pointerout", function(e) {
e.target.dataItem.dataContext.unhover();
});
legend.data.setAll(chart.series.values);
chart.appear(1000, 100);
#chartdiv {
width: 100%;
height: 300px;
}
<div id="chartdiv"></div>
<script src="https://cdn.amcharts.com/lib/5/index.js"></script>
<script src="https://cdn.amcharts.com/lib/5/xy.js"></script>
<script src="https://cdn.amcharts.com/lib/5/themes/Animated.js"></script>
英文:

It appears amcharts doesn't offer structured access to sub-objects in data through its data fields.

Thus, the only solution available is to preprocess the data; for your data structure, to cover both cases you could use something like

series.data.setAll(data
.map(o=&gt;Object.fromEntries(Object.entries(o)
.map(([k, v])=&gt;[k, v?.[0]?.aa ?? v]))
)
)

Snippet:
<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

var root = am5.Root.new(&quot;chartdiv&quot;);
root._logo.dispose();
root.setThemes([
am5themes_Animated.new(root)
]);
var chart = root.container.children.push(
am5xy.XYChart.new(root, {
panX: true,
panY: true,
wheelX: &quot;panX&quot;,
wheelY: &quot;zoomX&quot;,
layout: root.verticalLayout,
pinchZoomX:true
})
);
// Add cursor
// https://www.amcharts.com/docs/v5/charts/xy-chart/cursor/
var cursor = chart.set(&quot;cursor&quot;, am5xy.XYCursor.new(root, {
behavior: &quot;none&quot;
}));
cursor.lineY.set(&quot;visible&quot;, false);
// The data
var data = [{year:&quot;1930&quot;, italy:[{aa:20,bb:&quot;21&quot;}], germany:[{aa:30,bb:&quot;44&quot;}], uk:[{aa:40,bb:&quot;77&quot;}] },
{year: &quot;1934&quot;, italy: 1,germany: 2,uk: 6}, {year: &quot;1938&quot;,italy: 2,germany: 3,uk: 1}];
//var data = [ {year: &quot;1930&quot;,italy: 4,germany: 5.1,uk: 3}, {year: &quot;1934&quot;,italy: 1,germany: 2,uk: 6}, {year: &quot;1938&quot;,italy: 2,germany: 3,uk: 1} ];
// Create axes
// https://www.amcharts.com/docs/v5/charts/xy-chart/axes/
var xRenderer = am5xy.AxisRendererX.new(root, {});
xRenderer.grid.template.set(&quot;location&quot;, 0.5);
xRenderer.labels.template.setAll({
location: 0.5,
multiLocation: 0.5
});
var xAxis = chart.xAxes.push(
am5xy.CategoryAxis.new(root, {
categoryField: &quot;year&quot;,
renderer: xRenderer,
tooltip: am5.Tooltip.new(root, {})
})
);
xAxis.data.setAll(data);
var yAxis = chart.yAxes.push(
am5xy.ValueAxis.new(root, {
maxPrecision: 1,
renderer: am5xy.AxisRendererY.new(root, {
inversed: false
})
})
);
function createSeries(name, field) {
var series = chart.series.push(
am5xy.LineSeries.new(root, {
name: name,
xAxis: xAxis,
yAxis: yAxis,
valueYField: field,
categoryXField: &quot;year&quot;,
tooltip: am5.Tooltip.new(root, {
pointerOrientation: &quot;horizontal&quot;,
labelText: &quot;[bold]{name}[/]\n{categoryX}: {valueY}&quot;
})
})
);
series.bullets.push(function() {
return am5.Bullet.new(root, {
sprite: am5.Circle.new(root, {
radius: 5,
fill: series.get(&quot;fill&quot;)
})
});
});
series.set(&quot;setStateOnChildren&quot;, true);
series.states.create(&quot;hover&quot;, {});
series.mainContainer.set(&quot;setStateOnChildren&quot;, true);
series.mainContainer.states.create(&quot;hover&quot;, {});
series.strokes.template.states.create(&quot;hover&quot;, {
strokeWidth: 4
});
series.data.setAll(data.map(o=&gt;Object.fromEntries(Object.entries(o).map(([k, v])=&gt;[k, v?.[0]?.aa ?? v]))))
series.show(1000);
}
createSeries(&quot;Italy&quot;, &quot;italy&quot;);
createSeries(&quot;Germany&quot;, &quot;germany&quot;);
createSeries(&quot;UK&quot;, &quot;uk&quot;);
var legend = chart.children.push(
am5.Legend.new(root, {
centerX: am5.percent(90),
x: am5.percent(90),
centerY: am5.percent(105),
y: am5.percent(105),
})
);
// Make series change state when legend item is hovered
legend.itemContainers.template.states.create(&quot;hover&quot;, {});
legend.itemContainers.template.events.on(&quot;pointerover&quot;, function(e) {
e.target.dataItem.dataContext.hover();
});
legend.itemContainers.template.events.on(&quot;pointerout&quot;, function(e) {
e.target.dataItem.dataContext.unhover();
});
legend.data.setAll(chart.series.values);
chart.appear(1000, 100);

<!-- language: lang-css -->

 #chartdiv {
width: 100%;
height: 300px;
}

<!-- language: lang-html -->

&lt;div id=&quot;chartdiv&quot;&gt;&lt;/div&gt;
&lt;script src=&quot;https://cdn.amcharts.com/lib/5/index.js&quot;&gt;&lt;/script&gt;
&lt;script src=&quot;https://cdn.amcharts.com/lib/5/xy.js&quot;&gt;&lt;/script&gt;
&lt;script src=&quot;https://cdn.amcharts.com/lib/5/themes/Animated.js&quot;&gt;&lt;/script&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年5月21日 03:13:42
  • 转载请务必保留本文链接:https://go.coder-hub.com/76296962.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定