Beispiel #1
0
ScatterPlotExample::ScatterPlotExample(WContainerWidget *parent):
  WContainerWidget(parent)
{
  new WText(WString::tr("scatter plot 2"), this);

  WStandardItemModel *model = new WStandardItemModel(40, 2, this);
  model->setItemPrototype(new NumericItem());
  model->setHeaderData(0, WString("X"));
  model->setHeaderData(1, WString("Y = sin(X)"));

  for (unsigned i = 0; i < 40; ++i) {
    double x = (static_cast<double>(i) - 20) / 4;

    model->setData(i, 0, x);
    model->setData(i, 1, sin(x));
  }
 
  /*
   * Create the scatter plot.
   */
  WCartesianChart *chart = new WCartesianChart(this);
  chart->setModel(model);        // set the model
  chart->setXSeriesColumn(0);    // set the column that holds the X data
  chart->setLegendEnabled(true); // enable the legend

  chart->setType(ScatterPlot);   // set type to ScatterPlot

  // Typically, for mathematical functions, you want the axes to cross
  // at the 0 mark:
  chart->axis(XAxis).setLocation(ZeroValue);
  chart->axis(YAxis).setLocation(ZeroValue);

  // Automatically layout chart (space for axes, legend, ...)
  chart->setAutoLayoutEnabled();

  // Add the curves
  WDataSeries s(1, CurveSeries);
  s.setShadow(WShadow(3, 3, WColor(0, 0, 0, 127), 3));
  chart->addSeries(s);

  chart->resize(800, 300); // WPaintedWidget must be given explicit size

  chart->setMargin(10, Top | Bottom);            // add margin vertically
  chart->setMargin(WLength::Auto, Left | Right); // center horizontally

  ChartConfig *config = new ChartConfig(chart, this);
  config->setValueFill(ZeroValueFill);
}
Beispiel #2
0
WWidget *SysMon::Layout()
{
    Div *container = new Div("SysMon", "container");


    std::string htmlData;
    std::string file;
    if (m_cgiEnv->GetCurrentLanguage() == CgiEnv::Language::Fa) {
        file = "../templates/system-monitor-fa.wtml";
    } else {
        file = "../templates/system-monitor.wtml";
    }

    /// Read the template, otherwise return
    if (!CoreLib::FileSystem::Read(file, htmlData)) {
        return container;
    }


    /// Host Info
    m_pimpl->HostInfoDiv = new Div(container, "HostInfoDiv");


    /// CPU Model
    m_pimpl->CpuUsageModel = new WStandardItemModel(60, 7, this);
    m_pimpl->CpuUsageModel->setHeaderData(0, tr("system-monitor-cpu-percents-user"));
    m_pimpl->CpuUsageModel->setHeaderData(1, tr("system-monitor-cpu-percents-kernel"));
    m_pimpl->CpuUsageModel->setHeaderData(2, tr("system-monitor-cpu-percents-idle"));
    m_pimpl->CpuUsageModel->setHeaderData(3, tr("system-monitor-cpu-percents-iowait"));
    m_pimpl->CpuUsageModel->setHeaderData(4, tr("system-monitor-cpu-percents-swap"));
    m_pimpl->CpuUsageModel->setHeaderData(5, tr("system-monitor-cpu-percents-nice"));
    m_pimpl->CpuUsageModel->setHeaderData(6, WString("-"));


    /// Memory Model
    m_pimpl->MemoryUsageModel = new WStandardItemModel(60, 5, this);
    m_pimpl->MemoryUsageModel->setHeaderData(0, tr("system-monitor-memory-stats-total"));
    m_pimpl->MemoryUsageModel->setHeaderData(1, tr("system-monitor-memory-stats-free"));
    m_pimpl->MemoryUsageModel->setHeaderData(2, tr("system-monitor-memory-stats-used"));
    m_pimpl->MemoryUsageModel->setHeaderData(3, tr("system-monitor-memory-stats-cache"));
    m_pimpl->MemoryUsageModel->setHeaderData(4, WString("-"));


    /// Swap Model
    m_pimpl->SwapUsageModel = new WStandardItemModel(60, 4, this);
    m_pimpl->SwapUsageModel->setHeaderData(0, tr("system-monitor-swap-stats-total"));
    m_pimpl->SwapUsageModel->setHeaderData(1, tr("system-monitor-swap-stats-used"));
    m_pimpl->SwapUsageModel->setHeaderData(2, tr("system-monitor-swap-stats-free"));
    m_pimpl->SwapUsageModel->setHeaderData(3, WString("-"));


    /// Virtual Memory Model
    m_pimpl->VirtualMemoryUsageModel = new WStandardItemModel(60, 4, this);
    m_pimpl->VirtualMemoryUsageModel->setHeaderData(0, tr("system-monitor-virtual-memory-stats-total"));
    m_pimpl->VirtualMemoryUsageModel->setHeaderData(1, tr("system-monitor-virtual-memory-stats-used"));
    m_pimpl->VirtualMemoryUsageModel->setHeaderData(2, tr("system-monitor-virtual-memory-stats-free"));
    m_pimpl->VirtualMemoryUsageModel->setHeaderData(3, WString("-"));


    /// CPU Graph
    WCartesianChart *cpuUsageChart = new WCartesianChart();
    cpuUsageChart->setBackground(gray);
    cpuUsageChart->setModel(m_pimpl->CpuUsageModel);
    cpuUsageChart->setXSeriesColumn(m_pimpl->CpuUsageModel->columnCount() - 1);
    cpuUsageChart->setLegendEnabled(true);
    cpuUsageChart->setLegendLocation(LegendOutside, Right, AlignMiddle);

    cpuUsageChart->setType(ScatterPlot);
    cpuUsageChart->axis(XAxis).setScale(LinearScale);
    cpuUsageChart->axis(YAxis).setScale(LinearScale);

    cpuUsageChart->setPlotAreaPadding(120, Right);
    cpuUsageChart->setPlotAreaPadding(30, Top | Bottom);

    for (int i = 0; i < m_pimpl->CpuUsageModel->columnCount() - 1; ++i) {
        WDataSeries s(i, CurveSeries);
        s.setShadow(WShadow(3, 3, WColor(0, 0, 0, 127), 3));
        cpuUsageChart->addSeries(s);
    }

    cpuUsageChart->resize(600, 330);

    cpuUsageChart->setMargin(10, Top | Bottom);
    cpuUsageChart->setMargin(WLength::Auto, Left | Right);


    /// Memory Graph
    WCartesianChart *memoryUsageChart = new WCartesianChart();
    memoryUsageChart->setBackground(gray);
    memoryUsageChart->setModel(m_pimpl->MemoryUsageModel);
    memoryUsageChart->setXSeriesColumn(m_pimpl->MemoryUsageModel->columnCount() - 1);
    memoryUsageChart->setLegendEnabled(true);
    memoryUsageChart->setLegendLocation(LegendOutside, Right, AlignMiddle);

    memoryUsageChart->setType(ScatterPlot);
    memoryUsageChart->axis(XAxis).setScale(LinearScale);
    memoryUsageChart->axis(YAxis).setScale(LinearScale);

    memoryUsageChart->setPlotAreaPadding(220, Right);
    memoryUsageChart->setPlotAreaPadding(30, Top | Bottom);

    for (int i = 0; i < m_pimpl->MemoryUsageModel->columnCount() - 1; ++i) {
        WDataSeries s(i, CurveSeries);
        s.setShadow(WShadow(3, 3, WColor(0, 0, 0, 127), 3));
        memoryUsageChart->addSeries(s);
    }

    memoryUsageChart->resize(600, 330);

    memoryUsageChart->setMargin(10, Top | Bottom);
    memoryUsageChart->setMargin(WLength::Auto, Left | Right);


    /// Swap Graph
    WCartesianChart *swapUsageChart = new WCartesianChart();
    swapUsageChart->setBackground(gray);
    swapUsageChart->setModel(m_pimpl->SwapUsageModel);
    swapUsageChart->setXSeriesColumn(m_pimpl->SwapUsageModel->columnCount() - 1);
    swapUsageChart->setLegendEnabled(true);
    swapUsageChart->setLegendLocation(LegendOutside, Right, AlignMiddle);

    swapUsageChart->setType(ScatterPlot);
    swapUsageChart->axis(XAxis).setScale(LinearScale);
    swapUsageChart->axis(YAxis).setScale(LinearScale);

    swapUsageChart->setPlotAreaPadding(220, Right);
    swapUsageChart->setPlotAreaPadding(30, Top | Bottom);

    for (int i = 0; i < m_pimpl->SwapUsageModel->columnCount() - 1; ++i) {
        WDataSeries s(i, CurveSeries);
        s.setShadow(WShadow(3, 3, WColor(0, 0, 0, 127), 3));
        swapUsageChart->addSeries(s);
    }

    swapUsageChart->resize(600, 330);

    swapUsageChart->setMargin(10, Top | Bottom);
    swapUsageChart->setMargin(WLength::Auto, Left | Right);


    /// Virtual Memory Graph
    WCartesianChart *virtualMemoryUsageChart = new WCartesianChart();
    virtualMemoryUsageChart->setBackground(gray);
    virtualMemoryUsageChart->setModel(m_pimpl->VirtualMemoryUsageModel);
    virtualMemoryUsageChart->setXSeriesColumn(m_pimpl->VirtualMemoryUsageModel->columnCount() - 1);
    virtualMemoryUsageChart->setLegendEnabled(true);
    virtualMemoryUsageChart->setLegendLocation(LegendOutside, Right, AlignMiddle);

    virtualMemoryUsageChart->setType(ScatterPlot);
    virtualMemoryUsageChart->axis(XAxis).setScale(LinearScale);
    virtualMemoryUsageChart->axis(YAxis).setScale(LinearScale);

    virtualMemoryUsageChart->setPlotAreaPadding(220, Right);
    virtualMemoryUsageChart->setPlotAreaPadding(30, Top | Bottom);

    for (int i = 0; i < m_pimpl->VirtualMemoryUsageModel->columnCount() - 1; ++i) {
        WDataSeries s(i, CurveSeries);
        s.setShadow(WShadow(3, 3, WColor(0, 0, 0, 127), 3));
        virtualMemoryUsageChart->addSeries(s);
    }

    virtualMemoryUsageChart->resize(600, 330);

    virtualMemoryUsageChart->setMargin(10, Top | Bottom);
    virtualMemoryUsageChart->setMargin(WLength::Auto, Left | Right);


    /// Disk Info
    m_pimpl->DiskInfoDiv = new Div(container, "DiskInfoDiv");


    /// Net Info
    m_pimpl->NetworkInfoDiv = new Div(container, "NetworkInfoDiv");


    /// Fill the template
    WTemplate *tmpl = new WTemplate(container);
    tmpl->setTemplateText(WString(htmlData), TextFormat::XHTMLUnsafeText);

    tmpl->bindWidget("host-info", m_pimpl->HostInfoDiv);
    tmpl->bindWidget("cpu-info", cpuUsageChart);
    tmpl->bindWidget("memory-info", memoryUsageChart);
    tmpl->bindWidget("swap-info", swapUsageChart);
    tmpl->bindWidget("virtual-memory-info", virtualMemoryUsageChart);
    tmpl->bindWidget("disk-info", m_pimpl->DiskInfoDiv);
    tmpl->bindWidget("network-info", m_pimpl->NetworkInfoDiv);

    tmpl->bindWidget("host-info-title",
                     new WText(WString("<h4>{1}</h4>").arg(tr("system-monitor-host-info"))));
    tmpl->bindWidget("cpu-info-title",
                     new WText(WString("<h4>{1}</h4>").arg(tr("system-monitor-cpu-percents"))));
    tmpl->bindWidget("memory-info-title",
                     new WText(WString("<h4>{1}</h4>").arg(tr("system-monitor-memory-stats"))));
    tmpl->bindWidget("swap-info-title",
                     new WText(WString("<h4>{1}</h4>").arg(tr("system-monitor-swap-stats"))));
    tmpl->bindWidget("virtual-memory-info-title",
                     new WText(WString("<h4>{1}</h4>").arg(tr("system-monitor-virtual-memory-stats"))));
    tmpl->bindWidget("disk-info-title",
                     new WText(WString("<h4>{1}</h4>").arg(tr("system-monitor-disk-io-stats"))));
    tmpl->bindWidget("network-info-title",
                     new WText(WString("<h4>{1}</h4>").arg(tr("system-monitor-network-io-stats"))));

    return container;
}
Beispiel #3
0
TimeSeriesExample::TimeSeriesExample(Wt::WContainerWidget *parent):
  WContainerWidget(parent)
{
  new WText(WString::tr("scatter plot"), this);

  WAbstractItemModel *model = readCsvFile(
    WApplication::appRoot() + "timeseries.csv", this);

  if (!model)
    return;

  /*
   * Parses the first column as dates, to be able to use a date scale
   */
  for (int i = 0; i < model->rowCount(); ++i) {
    WString s = asString(model->data(i, 0));
    WDate d = WDate::fromString(s, "dd/MM/yy");
    model->setData(i, 0, d);
  }

  // Show a view that allows editing of the model.
  WContainerWidget *w = new WContainerWidget(this);
  WTableView *table = new WTableView(w);

  table->setMargin(10, Top | Bottom);
  table->setMargin(WLength::Auto, Left | Right);

  table->setModel(model);
  table->setSortingEnabled(false); // Does not make much sense for time series
  table->setColumnResizeEnabled(true);
  table->setSelectionMode(NoSelection);
  table->setAlternatingRowColors(true);
  table->setColumnAlignment(0, AlignCenter);
  table->setHeaderAlignment(0, AlignCenter);
  table->setRowHeight(22);

  // Editing does not really work without Ajax, it would require an
  // additional button somewhere to confirm the edited value.
  if (WApplication::instance()->environment().ajax()) {
    table->resize(800, 20 + 5*22);
    table->setEditTriggers(WAbstractItemView::SingleClicked);
  } else {
    table->resize(800, 20 + 5*22 + 25);
    table->setEditTriggers(WAbstractItemView::NoEditTrigger);
  }

  WItemDelegate *delegate = new WItemDelegate(this);
  delegate->setTextFormat("%.1f");
  table->setItemDelegate(delegate);
  table->setItemDelegateForColumn(0, new WItemDelegate(this));

  table->setColumnWidth(0, 80);
  for (int i = 1; i < model->columnCount(); ++i)
    table->setColumnWidth(i, 90);

  /*
   * Create the scatter plot.
   */
  WCartesianChart *chart = new WCartesianChart(this);
  //chart->setPreferredMethod(WPaintedWidget::PngImage);
  //chart->setBackground(gray);
  chart->setModel(model);        // set the model
  chart->setXSeriesColumn(0);    // set the column that holds the X data
  chart->setLegendEnabled(true); // enable the legend

  chart->setType(ScatterPlot);            // set type to ScatterPlot
  chart->axis(XAxis).setScale(DateScale); // set scale of X axis to DateScale

  // Automatically layout chart (space for axes, legend, ...)
  chart->setAutoLayoutEnabled();

  /*
   * Add first two columns as line series
   */
  for (int i = 1; i < 3; ++i) {
    WDataSeries s(i, LineSeries);
    s.setShadow(WShadow(3, 3, WColor(0, 0, 0, 127), 3));
    chart->addSeries(s);
  }

  chart->resize(800, 400); // WPaintedWidget must be given explicit size

  chart->setMargin(10, Top | Bottom);            // add margin vertically
  chart->setMargin(WLength::Auto, Left | Right); // center horizontally

  new ChartConfig(chart, this);
}