Skip to content

Commit

Permalink
fix(Legend): fix not getting data from dataset
Browse files Browse the repository at this point in the history
  • Loading branch information
SevenOutman committed Jun 16, 2022
1 parent 8607684 commit 071e358
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 20 deletions.
19 changes: 6 additions & 13 deletions src/charts/BarChart.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ import { EChartsContext } from '../constants';

const categoryAxisProps: any = {
type: 'category',
splitLine: false,
splitLine: false
};

const valueAxisProps: any = {
type: 'value',
type: 'value'
};

interface BarChartProps extends ChartComponentProps {
Expand Down Expand Up @@ -70,18 +70,11 @@ function BarChart(
function renderDefaultSeries() {
// 水平图表从上往下阅读则需将 data 翻转过来
const data = horizontal ? [...inputData!].reverse() : inputData!;
const values = data.map((d) => d[1]);
const values = data.map(d => d[1]);

return <Bars name={name} data={values} />;
}

function renderDefaultLegend() {
const components = Children.toArray(children);
const series = components.filter(isSeries);
const dataNames = series.length ? series.map((serie: any) => serie.name) : [name];
return <Legend data={dataNames} />;
}

const components = Children.toArray(children);
const series = components.filter(isSeries);

Expand All @@ -101,21 +94,21 @@ function BarChart(
{!categoryAxis && renderDefaultCategoryAxis()}
{!valueAxis && renderDefaultValueAxis()}
{!components.find((comp: any) => is(comp, 'bars')) && renderDefaultSeries()}
{legend && !components.find((comp: any) => is(comp, 'legend')) && renderDefaultLegend()}
{legend && !components.find((comp: any) => is(comp, 'legend')) && <Legend />}
{tooltip && <Tooltip />}
{components.map((child: any) => {
if (child.type === (horizontal ? YAxis : XAxis)) {
return cloneElement(child, {
...categoryAxisProps,
data: child.props.data || data!.map(([category]) => category),
data: child.props.data || data!.map(([category]) => category)
});
}
if (child.type === (horizontal ? XAxis : YAxis)) {
return cloneElement(child, valueAxisProps);
}
if (data!.length && isSeries(child) && !child.props.data) {
const serieIndex = series.indexOf(child);
return cloneElement(child, { data: data!.map((d) => d[serieIndex + 1]) });
return cloneElement(child, { data: data!.map(d => d[serieIndex + 1]) });
}
return child;
})}
Expand Down
14 changes: 7 additions & 7 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,16 +118,16 @@ const createOptions = {
},
[symbols.legend](option: any, props: any, context: any) {
function getOption() {
const { chartType, series, chartData = [] } = context;
const { chartType, } = context;
let legendOption: any = {
show: true,
bottom: 10,
data:
chartType === 'pie'
? chartData.map(([name]: any) => name)
: series.map((comp: any) => {
return comp.props.name;
}),
// data:
// chartType === 'pie'
// ? chartData.map(([name]: any) => name)
// : series.map((comp: any) => {
// return comp.props.name;
// }),
};

if (chartType === 'pie') {
Expand Down

0 comments on commit 071e358

Please sign in to comment.