Logarithmic axis in React Chart component
3 Feb 202624 minutes to read
The logarithmic axis uses a logarithmic scale and is particularly useful for visualizing data that spans a wide range of values. It effectively represents datasets that include both very small values (for example, 10-6) and very large values (for example, 106) within the same chart, improving readability and comparison.
To get start quickly with React Logarithmic Axis, you can check out this video:
import * as React from "react";
import * as ReactDOM from "react-dom";
import { ChartComponent, SeriesCollectionDirective, SeriesDirective, Inject, ColumnSeries, Legend, DateTime, Logarithmic, Tooltip, DataLabel, LineSeries } from '@syncfusion/ej2-react-charts';
import { logData } from './datasource';
function App() {
const primaryxAxis = { valueType: 'DateTime', title: 'Years', labelFormat: 'y' };
const primaryyAxis = { valueType: 'Logarithmic', title: 'Profit' };
return <ChartComponent id='charts' primaryXAxis={primaryxAxis} primaryYAxis={primaryyAxis} title='Product X Growth [1995-2005]'>
<Inject services={[ColumnSeries, Legend, Tooltip, DataLabel, Logarithmic, LineSeries, DateTime]}/>
<SeriesCollectionDirective>
<SeriesDirective dataSource={logData} xName='x' yName='y' name='Product X' type='Line'>
</SeriesDirective>
</SeriesCollectionDirective>
</ChartComponent>;
}
;
export default App;
ReactDOM.render(<App />, document.getElementById("charts"));import * as React from "react";
import * as ReactDOM from "react-dom";
import { AxisModel, ChartComponent, SeriesCollectionDirective, AxesDirective, AxisDirective, SeriesDirective, Inject,
ColumnSeries, Legend, DateTime, Logarithmic, Tooltip, DataLabel, Zoom, Crosshair, LineSeries, Selection}
from'@syncfusion/ej2-react-charts';
import { logData } from './datasource';
function App() {
const primaryxAxis: AxisModel = { valueType: 'DateTime', title: 'Years', labelFormat: 'y' };
const primaryyAxis: AxisModel = { valueType: 'Logarithmic', title: 'Profit' };
return <ChartComponent id='charts'
primaryXAxis={primaryxAxis}
primaryYAxis={primaryyAxis}
title='Product X Growth [1995-2005]'>
<Inject services={[ColumnSeries, Legend, Tooltip, DataLabel, Logarithmic, LineSeries, DateTime]} />
<SeriesCollectionDirective>
<SeriesDirective dataSource={logData} xName='x' yName='y' name='Product X' type='Line'>
</SeriesDirective>
</SeriesCollectionDirective>
</ChartComponent>
};
export default App;
ReactDOM.render(<App />, document.getElementById("charts"));export let logData = [
{ x: new Date(1995, 0, 1), y: 80 },
{ x: new Date(1996, 0, 1), y: 200 },
{ x: new Date(1997, 0, 1), y: 400 },
{ x: new Date(1998, 0, 1), y: 600 },
{ x: new Date(1999, 0, 1), y: 700 },
{ x: new Date(2000, 0, 1), y: 1400 },
{ x: new Date(2001, 0, 1), y: 2000 },
{ x: new Date(2002, 0, 1), y: 4000 },
{ x: new Date(2003, 0, 1), y: 6000 },
{ x: new Date(2004, 0, 1), y: 8000 },
{ x: new Date(2005, 0, 1), y: 11000 }
];export let logData: Object[] = [
{ x: new Date(1995, 0, 1), y: 80 },
{ x: new Date(1996, 0, 1), y: 200 },
{ x: new Date(1997, 0, 1), y: 400 },
{ x: new Date(1998, 0, 1), y: 600 },
{ x: new Date(1999, 0, 1), y: 700 },
{ x: new Date(2000, 0, 1), y: 1400 },
{ x: new Date(2001, 0, 1), y: 2000 },
{ x: new Date(2002, 0, 1), y: 4000 },
{ x: new Date(2003, 0, 1), y: 6000 },
{ x: new Date(2004, 0, 1), y: 8000 },
{ x: new Date(2005, 0, 1), y: 11000 }
];Note: To use the logarithmic axis, inject
Logarithmicinto theprovideoption and set thevalueTypeproperty of the axis toLogarithmic.
Range
The range of the logarithmic axis is calculated automatically based on the provided data. This automatic calculation ensures that all data points are visible within the chart area. The axis range can also be customized explicitly using the minimum, maximum, and interval properties.
import * as React from "react";
import * as ReactDOM from "react-dom";
import { ChartComponent, SeriesCollectionDirective, SeriesDirective, Inject, ColumnSeries, Legend, DateTime, Logarithmic, Tooltip, DataLabel, LineSeries } from '@syncfusion/ej2-react-charts';
import { logData } from './datasource';
function App() {
const primaryxAxis = { valueType: 'DateTime', title: 'Years', labelFormat: 'y' };
const primaryyAxis = { valueType: 'Logarithmic', title: 'Profit', minimum: 100, maximum: 10000 };
return <ChartComponent id='charts' primaryXAxis={primaryxAxis} primaryYAxis={primaryyAxis} title='Product X Growth [1995-2005]'>
<Inject services={[ColumnSeries, Legend, Tooltip, DataLabel, Logarithmic, LineSeries, DateTime]}/>
<SeriesCollectionDirective>
<SeriesDirective dataSource={logData} xName='x' yName='y' name='Product X' type='Line'>
</SeriesDirective>
</SeriesCollectionDirective>
</ChartComponent>;
}
;
export default App;
ReactDOM.render(<App />, document.getElementById("charts"));import * as React from "react";
import * as ReactDOM from "react-dom";
import { AxisModel, ChartComponent, SeriesCollectionDirective, AxesDirective, AxisDirective, SeriesDirective, Inject,
ColumnSeries, Legend, DateTime, Logarithmic, Tooltip, DataLabel, Zoom, Crosshair, LineSeries, Selection}
from'@syncfusion/ej2-react-charts';
import { logData } from './datasource';
function App() {
const primaryxAxis: AxisModel = { valueType: 'DateTime', title: 'Years', labelFormat: 'y' };
const primaryyAxis: AxisModel = { valueType: 'Logarithmic', title: 'Profit', minimum: 100, maximum: 10000 };
return <ChartComponent id='charts'
primaryXAxis={primaryxAxis}
primaryYAxis={primaryyAxis}
title='Product X Growth [1995-2005]'>
<Inject services={[ColumnSeries, Legend, Tooltip, DataLabel, Logarithmic, LineSeries, DateTime]} />
<SeriesCollectionDirective>
<SeriesDirective dataSource={logData} xName='x' yName='y' name='Product X' type='Line'>
</SeriesDirective>
</SeriesCollectionDirective>
</ChartComponent>
};
export default App;
ReactDOM.render(<App />, document.getElementById("charts"));export let logData = [
{ x: new Date(1995, 0, 1), y: 80 },
{ x: new Date(1996, 0, 1), y: 200 },
{ x: new Date(1997, 0, 1), y: 400 },
{ x: new Date(1998, 0, 1), y: 600 },
{ x: new Date(1999, 0, 1), y: 700 },
{ x: new Date(2000, 0, 1), y: 1400 },
{ x: new Date(2001, 0, 1), y: 2000 },
{ x: new Date(2002, 0, 1), y: 4000 },
{ x: new Date(2003, 0, 1), y: 6000 },
{ x: new Date(2004, 0, 1), y: 8000 },
{ x: new Date(2005, 0, 1), y: 11000 }
];export let logData: Object[] = [
{ x: new Date(1995, 0, 1), y: 80 },
{ x: new Date(1996, 0, 1), y: 200 },
{ x: new Date(1997, 0, 1), y: 400 },
{ x: new Date(1998, 0, 1), y: 600 },
{ x: new Date(1999, 0, 1), y: 700 },
{ x: new Date(2000, 0, 1), y: 1400 },
{ x: new Date(2001, 0, 1), y: 2000 },
{ x: new Date(2002, 0, 1), y: 4000 },
{ x: new Date(2003, 0, 1), y: 6000 },
{ x: new Date(2004, 0, 1), y: 8000 },
{ x: new Date(2005, 0, 1), y: 11000 }
];Logarithmic base
The logarithmic base of the axis can be customized using the logBase property. This property determines how the axis values are distributed along the scale.
For example, when the logarithmic base is set to 5, the axis values follow the sequence 5-2, 5-1, 50, 51, 52, and so on.
import * as React from "react";
import * as ReactDOM from "react-dom";
import { ChartComponent, SeriesCollectionDirective, SeriesDirective, Inject, ColumnSeries, Legend, DateTime, Logarithmic, Tooltip, DataLabel, LineSeries } from '@syncfusion/ej2-react-charts';
import { logData } from './datasource';
function App() {
const primaryxAxis = { valueType: 'DateTime', title: 'Years', labelFormat: 'y' };
const primaryyAxis = { valueType: 'Logarithmic', title: 'Profit', minimum: 100, maximum: 10000, logBase: 2 };
return <ChartComponent id='charts' primaryXAxis={primaryxAxis} primaryYAxis={primaryyAxis} title='Product X Growth [1995-2005]'>
<Inject services={[ColumnSeries, Legend, Tooltip, DataLabel, Logarithmic, LineSeries, DateTime]}/>
<SeriesCollectionDirective>
<SeriesDirective dataSource={logData} xName='x' yName='y' name='Product X' type='Line'>
</SeriesDirective>
</SeriesCollectionDirective>
</ChartComponent>;
}
;
export default App;
ReactDOM.render(<App />, document.getElementById("charts"));import * as React from "react";
import * as ReactDOM from "react-dom";
import { AxisModel, ChartComponent, SeriesCollectionDirective, AxesDirective, AxisDirective, SeriesDirective, Inject,
ColumnSeries, Legend, DateTime, Logarithmic, Tooltip, DataLabel, Zoom, Crosshair, LineSeries, Selection}
from'@syncfusion/ej2-react-charts';
import { logData } from './datasource';
function App() {
const primaryxAxis: AxisModel = { valueType: 'DateTime', title: 'Years', labelFormat: 'y' };
const primaryyAxis: AxisModel = { valueType: 'Logarithmic', title: 'Profit', minimum: 100, maximum: 10000, logBase: 2 };
return <ChartComponent id='charts'
primaryXAxis={primaryxAxis}
primaryYAxis={primaryyAxis}
title='Product X Growth [1995-2005]'>
<Inject services={[ColumnSeries, Legend, Tooltip, DataLabel, Logarithmic, LineSeries, DateTime]} />
<SeriesCollectionDirective>
<SeriesDirective dataSource={logData} xName='x' yName='y' name='Product X' type='Line'>
</SeriesDirective>
</SeriesCollectionDirective>
</ChartComponent>
};
export default App;
ReactDOM.render(<App />, document.getElementById("charts"));export let logData = [
{ x: new Date(1995, 0, 1), y: 80 },
{ x: new Date(1996, 0, 1), y: 200 },
{ x: new Date(1997, 0, 1), y: 400 },
{ x: new Date(1998, 0, 1), y: 600 },
{ x: new Date(1999, 0, 1), y: 700 },
{ x: new Date(2000, 0, 1), y: 1400 },
{ x: new Date(2001, 0, 1), y: 2000 },
{ x: new Date(2002, 0, 1), y: 4000 },
{ x: new Date(2003, 0, 1), y: 6000 },
{ x: new Date(2004, 0, 1), y: 8000 },
{ x: new Date(2005, 0, 1), y: 11000 }
];export let logData: Object[] = [
{ x: new Date(1995, 0, 1), y: 80 },
{ x: new Date(1996, 0, 1), y: 200 },
{ x: new Date(1997, 0, 1), y: 400 },
{ x: new Date(1998, 0, 1), y: 600 },
{ x: new Date(1999, 0, 1), y: 700 },
{ x: new Date(2000, 0, 1), y: 1400 },
{ x: new Date(2001, 0, 1), y: 2000 },
{ x: new Date(2002, 0, 1), y: 4000 },
{ x: new Date(2003, 0, 1), y: 6000 },
{ x: new Date(2004, 0, 1), y: 8000 },
{ x: new Date(2005, 0, 1), y: 11000 }
];Logarithmic interval
The spacing between logarithmic axis labels can be controlled using the interval property. This property defines the step size between consecutive logarithmic values.
For example, when the logarithmic base is 10 and the interval is set to 2, the axis labels are placed at values such as 102, 104, and 106. The default value of the interval is 1.
import * as React from "react";
import * as ReactDOM from "react-dom";
import { ChartComponent, SeriesCollectionDirective, SeriesDirective, Inject, ColumnSeries, Legend, DateTime, Logarithmic, Tooltip, DataLabel, LineSeries } from '@syncfusion/ej2-react-charts';
import { logData } from './datasource';
function App() {
const primaryxAxis = { valueType: 'DateTime', title: 'Years', labelFormat: 'y' };
const primaryyAxis = { valueType: 'Logarithmic', title: 'Profit', minimum: 100, maximum: 10000, interval: 2 };
return <ChartComponent id='charts' primaryXAxis={primaryxAxis} primaryYAxis={primaryyAxis} title='Product X Growth [1995-2005]'>
<Inject services={[ColumnSeries, Legend, Tooltip, DataLabel, Logarithmic, LineSeries, DateTime]}/>
<SeriesCollectionDirective>
<SeriesDirective dataSource={logData} xName='x' yName='y' name='Product X' type='Line'>
</SeriesDirective>
</SeriesCollectionDirective>
</ChartComponent>;
}
;
export default App;
ReactDOM.render(<App />, document.getElementById("charts"));import * as React from "react";
import * as ReactDOM from "react-dom";
import { AxisModel, ChartComponent, SeriesCollectionDirective, AxesDirective, AxisDirective, SeriesDirective, Inject,
ColumnSeries, Legend, DateTime, Logarithmic, Tooltip, DataLabel, Zoom, Crosshair, LineSeries, Selection}
from'@syncfusion/ej2-react-charts';
import { logData } from './datasource';
function App() {
const primaryxAxis: AxisModel = { valueType: 'DateTime', title: 'Years', labelFormat: 'y' };
const primaryyAxis: AxisModel = { valueType: 'Logarithmic', title: 'Profit', minimum: 100, maximum: 10000, interval: 2 };
return <ChartComponent id='charts'
primaryXAxis={primaryxAxis}
primaryYAxis={primaryyAxis}
title='Product X Growth [1995-2005]'>
<Inject services={[ColumnSeries, Legend, Tooltip, DataLabel, Logarithmic, LineSeries, DateTime]} />
<SeriesCollectionDirective>
<SeriesDirective dataSource={logData} xName='x' yName='y' name='Product X' type='Line'>
</SeriesDirective>
</SeriesCollectionDirective>
</ChartComponent>
};
export default App;
ReactDOM.render(<App />, document.getElementById("charts"));export let logData = [
{ x: new Date(1995, 0, 1), y: 80 },
{ x: new Date(1996, 0, 1), y: 200 },
{ x: new Date(1997, 0, 1), y: 400 },
{ x: new Date(1998, 0, 1), y: 600 },
{ x: new Date(1999, 0, 1), y: 700 },
{ x: new Date(2000, 0, 1), y: 1400 },
{ x: new Date(2001, 0, 1), y: 2000 },
{ x: new Date(2002, 0, 1), y: 4000 },
{ x: new Date(2003, 0, 1), y: 6000 },
{ x: new Date(2004, 0, 1), y: 8000 },
{ x: new Date(2005, 0, 1), y: 11000 }
];export let logData: Object[] = [
{ x: new Date(1995, 0, 1), y: 80 },
{ x: new Date(1996, 0, 1), y: 200 },
{ x: new Date(1997, 0, 1), y: 400 },
{ x: new Date(1998, 0, 1), y: 600 },
{ x: new Date(1999, 0, 1), y: 700 },
{ x: new Date(2000, 0, 1), y: 1400 },
{ x: new Date(2001, 0, 1), y: 2000 },
{ x: new Date(2002, 0, 1), y: 4000 },
{ x: new Date(2003, 0, 1), y: 6000 },
{ x: new Date(2004, 0, 1), y: 8000 },
{ x: new Date(2005, 0, 1), y: 11000 }
];