Skip to content

Commit c60f9b2

Browse files
authored
Merge pull request #3 from Practically/v1.0
V1.0
2 parents 2bb4cfe + a1bc59e commit c60f9b2

File tree

2 files changed

+76
-0
lines changed

2 files changed

+76
-0
lines changed

src/Dataset.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,22 @@ class Dataset extends \yii\base\Component
9696
'rgba(255, 159, 64, 1)'
9797
];
9898

99+
/**
100+
* The fill for the dataset
101+
*
102+
* @see https://www.chartjs.org/docs/latest/charts/area.html#filling-modes
103+
*
104+
* @var mixed
105+
*/
106+
public $fill = null;
107+
108+
/**
109+
* Label for the dataset
110+
*
111+
* @var string
112+
*/
113+
public $label = '';
114+
99115
/**
100116
* Executes the query and populates the data with the result of the query
101117
*
@@ -146,6 +162,14 @@ public function getDataset()
146162
$dataset['borderWidth'] = $this->borderWidth;
147163
}
148164

165+
if ($this->fill !== null) {
166+
$dataset['fill'] = $this->fill;
167+
}
168+
169+
if (strlen($this->label) > 0) {
170+
$dataset['label'] = $this->label;
171+
}
172+
149173
return $dataset;
150174
}
151175

@@ -200,6 +224,8 @@ public function addBarColors($dataset, $attribute, $label)
200224

201225
$i += count($this->$attribute);
202226
}
227+
} elseif (is_string($this->$attribute)) {
228+
$dataset[$label] = $this->$attribute;
203229
}
204230

205231
return $dataset;

tests/DatasetTest.php

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,4 +96,54 @@ public function testColorCount(): void
9696
count($dataset['backgroundColor'])
9797
);
9898
}
99+
100+
/**
101+
* Test that you can use string as colors for line charts
102+
*
103+
* @return void
104+
*/
105+
public function testColorsAsStrings(): void
106+
{
107+
$this->dataset->backgroundColors = 'red';
108+
$this->dataset->borderColors = 'blue';
109+
$dataset = $this->dataset->getDataset();
110+
111+
$this->assertEquals('red', $dataset['backgroundColor']);
112+
$this->assertEquals('blue', $dataset['borderColor']);
113+
}
114+
115+
/**
116+
* Tests you can set the fill property of the dataset
117+
*
118+
* @return void
119+
*/
120+
public function testDatasetFill(): void
121+
{
122+
$this->assertArrayNotHasKey(
123+
'fill',
124+
$this->dataset->getDataset()
125+
);
126+
127+
$this->dataset->fill = false;
128+
$this->assertFalse($this->dataset->getDataset()['fill']);
129+
}
130+
131+
/**
132+
* Test you can add a label to the dataset
133+
*
134+
* @return void
135+
*/
136+
public function testAddingLabelsToDatasets(): void
137+
{
138+
$this->assertArrayNotHasKey(
139+
'label',
140+
$this->dataset->getDataset()
141+
);
142+
143+
$this->dataset->label = 'My New Label';
144+
$this->assertEquals(
145+
'My New Label',
146+
$this->dataset->getDataset()['label']
147+
);
148+
}
99149
}

0 commit comments

Comments
 (0)