//柱状图
void RadioDataForm::drawPulseWidthStatisticsGraph(QVector<double> values, double minPw, double maxPw)
{
//清空原有图形设置
ui->widget_pwStatisticsPlot->clearGraphs();
ui->widget_pwStatisticsPlot->clearPlottables();
int histogramSize = values.size();
QCPBars *bar = new QCPBars(ui->widget_pwStatisticsPlot->xAxis, ui->widget_pwStatisticsPlot->yAxis);
bar->setWidthType(QCPBars::wtPlotCoords);
// bar->setWidth(1.0); //设置各柱之间无空隙
bar->setBrush(Qt::blue);
//计算绘图数据
QVector<double> index;
index.resize(histogramSize);
for(int i = 1; i <= histogramSize; i++){
index[i-1] = i; //x轴数据从1~histogramSize
}
bar->setData(index, values);
//保存数据用于鼠标点击显示
m_pwHistNum = histogramSize;
m_pwHistValue = values;
m_pwLower = minPw;
m_pwUpper = maxPw;
//调整x轴显示范围,使得最左边柱状图左边线和最右边柱状图右边线显示出来
//必须先rescale坐标轴,使得坐标轴按数据设置好后再进行调整
ui->widget_pwStatisticsPlot->rescaleAxes();
QCPRange xRange = ui->widget_pwStatisticsPlot->xAxis->range();
double barWidth = bar->width();
ui->widget_pwStatisticsPlot->xAxis->setRange(xRange.lower-0.5*barWidth, xRange.upper+0.5*barWidth);
//调整X轴标签显示
QVector<QString> labels;
QVector<double> xPos;
m_xTickNumLess = 10;
labels.resize(m_xTickNumLess);
xPos.resize(m_xTickNumLess);
double posSpan = qFloor(1.0*histogramSize/m_xTickNumLess); //显示部分柱标签
double labelSpan = (maxPw-minPw)/histogramSize*posSpan;
for(int i = 0; i < m_xTickNumLess; i++){
xPos[i] = i*posSpan + 1 /*+ qFloor(posSpan/2.0)*/; //刻度位置挪到图中间
//柱标签为统计区间的中间位置所代表的值
labels[i] = QString::number(minPw + i*labelSpan /*+ (0.5+qFloor(posSpan/2.0))*(maxPw-minPw)/histogramSize*/, 'f', 2) + "us";
}
QSharedPointer<QCPAxisTickerText> xTicker(new QCPAxisTickerText);
xTicker->addTicks(xPos, labels);
ui->widget_pwStatisticsPlot->xAxis->setTicker(xTicker);
ui->widget_pwStatisticsPlot->replot();
}
//双线图
void RadioDataForm::UpdateValues(QVector<double> values)
{
bool max_value_change_flag=false;
bool min_value_change_flag=false;
int count=values.count();
if(count!=cur_line_count)
{
return;
}
max_value=value_list[0].max_value;
min_value=value_list[0].min_value;
for(int i=0;i<count;i++)
{
//设置值
SetOneValue(i,values.at(i));
if(max_value<value_list[i].max_value)
{
max_value=value_list[i].max_value;
max_value_change_flag=true;
}
if(min_value>value_list[i].min_value)
{
min_value=value_list[i].min_value;
min_value_change_flag=true;
}
}
//y坐标轴更新
if(max_value_change_flag==true)
{
ui->widget_amplitude->yAxis->setRangeUpper(max_value+20);
}
if(min_value_change_flag==true)
{
ui->widget_amplitude->yAxis->setRangeLower(min_value-20);
}
}
void RadioDataForm::SetOneValue(int number,double value)
{
value_list[number].value_vector.push_back(value);
max_second_count[number]++;
int count_1=value_list[number].value_vector.count();
value_list[number].time_t_vector.push_back(max_second_count[number]);
int count_2=value_list[number].time_t_vector.count();
if(count_1!=count_2)
{
value_list[number].value_vector.clear();
value_list[number].time_t_vector.clear();
return;
}
//一根线上的最大点数目
if(count_1>MAX_POINT_NUM)//超过最大值,移除第一个点
{
value_list[number].value_vector.removeFirst();
value_list[number].time_t_vector.removeFirst();
}
count_1=value_list[number].value_vector.count();
value_list[number].max_value=value_list[number].value_vector.at(0);
value_list[number].min_value=value_list[number].value_vector.at(0);
if(m_iPaintNum>MAX_POINT_NUM)
{
//x坐标更新
ui->widget_amplitude->xAxis->setRange(max_second_count[number],max_second_count[number]+50);
ui->widget_amplitude->replot();
m_iPaintNum=0;
}
ui->widget_amplitude->graph(number)->setData(value_list[number].time_t_vector,value_list[number].value_vector);
ui->widget_amplitude->replot();
m_iPaintNum++;
//x轴标签
/* QCPBars *bar = new QCPBars(ui->widget_freqStatisticsPlot->xAxis, ui->widget_freqStatisticsPlot->yAxis);
bar->setWidthType(QCPBars::wtPlotCoords);
ui->widget_amplitude->rescaleAxes();
QCPRange xRange = ui->widget_amplitude->xAxis->range();
double barWidth = bar->width();
ui->widget_amplitude->xAxis->setRange(xRange.lower-0.5*barWidth, xRange.upper+0.5*barWidth);
QVector<QString>labels;
labels.resize(MAX_POINT_NUM);
for(int i=0;i<MAX_POINT_NUM;i++)
{
labels[i] = QString::number(i);
}
QSharedPointer<QCPAxisTickerText> xTicker(new QCPAxisTickerText);
xTicker->addTicks(value_list[number].time_t_vector, labels);
ui->widget_amplitude->xAxis->setTicker(xTicker);
ui->widget_amplitude->replot();*/
//大小值
for(int i=0;i<count_1;i++)
{
if(value_list[number].max_value<value_list[number].value_vector.at(i))
{
value_list[number].max_value=value_list[number].value_vector.at(i);
}
if(value_list[number].min_value>value_list[number].value_vector.at(i))
{
value_list[number].min_value=value_list[number].value_vector.at(i);
}
}
//把值刷在
QString temp_value_str=QString::number(value,'f',2);
ui->widget_amplitude->graph(number)->setName(title_list.at(number)+":"+temp_value_str);
}