Reputation: 249
My purpose is to create two types graphics in php: pie, linear and represent data in each other.
Each graph should have own settings representation like: labels, texts.
In pie graph is required to display number of indicator as number or in percent.
I have begun with defenition of interface and abstract class:
interface IGraph {
function percent();
function percentGroup($type);
}
abstract class Graph implements IGraph {
abstract function source();
abstract function percent();
abstract function percentGroup($type);
}
class GraphPie extends Graph {
function source() {}
function percent() {}
function percentGroup($type) {}
}
I realized that should be a common abstract class Graph
and custom classes as GraphPie
, GraphLinear
.
My problem is to create whole structure of classes for this task. For example, need I create another class as GraphModelClass
where to write methods for retrieve data from db?
In other words I need to build functionality when I retrieve data by schema and can represent this in any graph.
I tried to adopt patterns in my case. I have reviewed patterns like: factory, facade, but it is not fit for me.
I follow this short instruction about using patterns:
Upvotes: 3
Views: 458
Reputation: 2958
Generally speaking, you only need an interface
if your code is going to rely on different implementations of other code, that you or someone else wrote. You could use an interface
if you're going to implement a single view for multiple data models. The view could then access the model which has its contract in the form of an interface.
But in your case you want to do it the other way around: to use a single model for the aggregated data with percentages and group names, with multiple views (line graph, pie chart) for the same kind of data.
You do not need to use object inheritance for that. Create a model to retrieve and store the data from the database. Then use that same model in each of your view classes that draw a line graph or a pie chart.
If your classes are named GraphLine
and GraphPie
, you could use single inheritance like this:
class GraphBase { /* generic draw functions, like labels */ }
class GraphLine extends GraphBase { /* Line graph specific drawing */ }
class GraphPie extends GraphBase { /* Pie chart specific drawing */ }
For the data, you could create a class DataModel
like this
class DataModel {
protected $iYear;
public function setYear(int $iYear) : self {}
public function getGraphData() : array { /* SQL SELECT .. GROUP BY .. */ }
}
And then in GraphBase
:
class GraphBase {
protected $oDataModel;
public function setDataModel(DataModel $oDataModel) : self {
$this->oDataModel = $oDataModel;
return $this;
}
abstract function draw(); // GraphPie and GraphLine should implement this
}
Usage:
$Model = new DataModel;
$Model->setYear(2017);
$LineGraph = new GraphLine;
$LineGraph
->setDataModel($Model)
->draw();
Upvotes: 2
Reputation: 605
As you're not going to be using it in a commercial context then you might want to consider using a ready-made library such as for example JpGraph When using any 3rd party library, always check the licencing in case a paid for licence is required for commercial use of a library
Upvotes: 0