LineChart
Overview #
A line chart is a way of plotting data points on a line. Often, it is used to show trend data, or the comparison of two data sets.
Code:
<?php
\koolreport\chartjs\LineChart::create(array(
"title"=>"Sale vs Cost",
"dataSource"=>$this->dataStore('month_sales'),
"columns"=>array(
"month",
"sale"=>array("label"=>"Sale","type"=>"number","prefix"=>"$"),
"cost"=>array("label"=>"Cost","type"=>"number","prefix"=>"$"),
)
));
Above example show miminum settings to setup a line chart. We use 2 columns from datasource which are "category' and "amount" to draw chart.
Note that: we may add extra column and the extra column will represent another series on the chart. The first column is used for xAxis labeling.
Settings #
name | type | default | description |
---|---|---|---|
name | string | Set name for chart. It is optional | |
title | string | Title of the chart | |
dataSource | mixed | Set the data source for chart, accepting DataStore, DataSource, Process object and even array in table or associate format | |
columns | array | List of columns used to draw chart | |
options | array | Extra options for line chart or area chart, please view options section for more details | |
backgroundOpacity | number | 0.5 | Set the opacity for background |
axes | array | Defined multiple axes for your chart |
Properties of a column #
Except for the first column, the next column in the columns
list will represent a series of data on the chart. We may have further settings for each column. Below are properties we can set for column.
Example
<?php
LineChart::create(array(
"dataSource"=>$this->dataStore("category_amount"),
"columns"=>array(
"category",
"amount"=>array(
"label"=>"Amount"
"type"=>"number",
"prefix"=>"$",
"decimals"=>2,
)
)
))
?>
name | type | default | description |
---|---|---|---|
label | string | Set label for column | |
type | string | Type of columns number ,string ,datetime | |
prefix | string | Set prefix for value, for example $ | |
suffix | string | Set suffix for value | |
formatValue | string/function | Accept string or function. For example: "formatValue"=>"@value USD" or "formatValue"=>function($value){return $value." USD";} | |
decimals | number | The number of number after decimal points | |
thousandSeparator | string | , | Specify how thousand is separated |
decimalPoint | string | . | Specify decimal point |
config | array | Contain special settings of chart js config for series, see below ChartJs configuration for column for more details | |
chartType | string | Specify which type of chart a column/series should be drawn as | |
axis | string | Specify which axis a column/series is measured against |
Since version 3.0.0, you can set Chartjs' dataset config directly in column settings instead of in column's config:
BarChart::create(array(
...
"columns" => array(
"column_1" => array(
...
"borderColor" => "blue", // set borderColor directly for column_1 instead of column_1's config
)
)
))
Config for column #
This is optional but we can have many further options/configs for the chart. Below are the properties that you can put under the config
.
<?php
LineChart::create(array(
"dataSource"=>$this->dataStore("category_amount"),
"columns"=>array(
"category",
"amount"=>array(
"label"=>"Amount"
"type"=>"number",
"prefix"=>"$",
"config"=>array(
"steppedLine"=>true
)
)
)
))
?>
name | type | default | description |
---|---|---|---|
backgroundColor | string | The fill color under the line. | |
borderColor | string | The color of the line | |
borderWidth | number | The width of the line in pixels. | |
borderDash | number[] | Length and spacing of dashes. | |
borderDashOffset | number | Offset for line dashes | |
borderCapStyle | string | Cap style of the line. | |
borderJoinStyle | string | Line joint style. | |
cubicInterpolationMode | string | Algorithm used to interpolate a smooth curve from the discrete data points. | |
fill | boolean/string | How to fill the area under the line. | |
lineTension | number | Bezier curve tension of the line. Set to 0 to draw straightlines. This option is ignored if monotone cubic interpolation is used. | |
pointBackgroundColor | number/number[] | The fill color for points. | |
pointBorderColor | string/string[] | The border color for points. | |
pointBorderWidth | number/array | The width of the point border in pixels. | |
pointRadius | number/array | The radius of the point shape. If set to 0, the point is not rendered. | |
pointStyle | string/array | Style of the point. | |
pointHitRadius | number/array | The pixel size of the non-displayed point that reacts to mouse events.d | |
pointHoverBackgroundColor | string/array | Point background color when hovered. | |
pointHoverBorderColor | string/array | Point border color when hovered. | |
pointHoverBorderWidth | numner/array | Border width of point when hovered. | |
pointHoverRadius | number/array | The radius of the point when hovered. | |
showLine | boolean | If false, the line is not drawn for this dataset. | |
spanGaps | boolean | If true, lines will be drawn between points with no or null data. If false, points with NaN data will create a break in the line | |
steppedLine | boolean/string | If the line is shown as a stepped line. Value: true , false , "before" , "after" . If the steppedLine value is set to anything other than false, lineTension will be ignored. |
Get started with KoolReport
KoolReport will help you to construct good php data report by gathering your data from multiple sources, transforming them into valuable insights, and finally visualizing them in stunning charts and graphs.