{ "cells": [ { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "#include \"random.hpp\"\n", "#include \"xplot/xfigure.hpp\"\n", "#include \"xplot/xmarks.hpp\"\n", "#include \"xplot/xaxes.hpp\"" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Basic Scatter" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "std::size_t size = 10;\n", "std::vector x_data(size);\n", "std::iota(x_data.begin(), x_data.end(), 0);\n", "std::vector y_data = randn(size);" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "xpl::linear_scale sx, sy;\n", "auto ax_x = xpl::axis_generator(sx)\n", " .label(\"x\")\n", " .finalize();\n", "auto ax_y = xpl::axis_generator(sy)\n", " .label(\"y\")\n", " .orientation(\"vertical\")\n", " .side(\"left\")\n", " .finalize();" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "auto scatter1 = xpl::scatter_generator(sx, sy)\n", " .x(x_data)\n", " .y(y_data)\n", " .finalize();" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "auto fig1 = xpl::figure_generator()\n", " .padding_x(0.025)\n", " .padding_y(0.025)\n", " .finalize();\n", "fig1.add_mark(scatter1);\n", "fig1.add_axis(ax_x);\n", "fig1.add_axis(ax_y);\n", "fig1" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Changing the marker and adding text to each point of the scatter" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "scatter1.names = std::vector{\"0\", \"1\", \"2\", \"3\", \"4\", \"5\", \"6\", \"7\", \"8\", \"9\"};" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "scatter1.colors = std::vector>{\"red\"};" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "scatter1.marker = \"cross\";" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Changing the opacity of each marker" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "scatter1.default_opacities = std::vector({0.3, 0.5, 1.});" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Representing additional dimensions of data" ] }, { "cell_type": "markdown", "metadata": { "input_collapsed": false }, "source": [ "## Linear Scale for Color Data" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "std::vector c_data = randn(size);" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "xpl::color_scale sc;" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "auto scatter2 = xpl::scatter_generator(sx, sy, sc)\n", " .x(x_data)\n", " .y(y_data)\n", " .color(c_data)\n", " .stroke(\"black\")\n", " .finalize();" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "auto ax_c = xpl::color_axis_generator(sc)\n", " .label(\"Values\")\n", " .orientation(\"vertical\")\n", " .side(\"right\")\n", " .finalize();" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "auto fig2 = xpl::figure_generator()\n", " .padding_x(0.025)\n", " .fig_margin(xeus::xjson({{\"top\", 50}, {\"bottom\", 70}, {\"left\", 50}, {\"right\", 100}}))\n", " .finalize();\n", "fig2.add_mark(scatter2);\n", "fig2.add_axis(ax_x);\n", "fig2.add_axis(ax_y);\n", "fig2.add_axis(ax_c);\n", "fig2" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "// setting the fill to be empty\n", "scatter2.stroke = xtl::xoptional{};\n", "scatter2.fill = false;" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "// Setting the fill back\n", "scatter2.stroke = \"black\";\n", "scatter2.fill = true;" ] }, { "cell_type": "markdown", "metadata": { "input_collapsed": false }, "source": [ "## Ordinal Scale for Color" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "std::vector c2_data = randint(0, 5, size);" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "auto sc1 = sc;\n", "sc1.colors = std::vector{\"DodgerBlue\", \"SeaGreen\", \"Yellow\", \"HotPink\", \"OrangeRed\"};" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "auto scatter3 = xpl::scatter_generator(sx, sy, sc1)\n", " .x(x_data)\n", " .y(y_data)\n", " .color(c2_data)\n", " .stroke(\"black\")\n", " .finalize();" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "auto ax2_c = xpl::color_axis_generator(sc1)\n", " .label(\"Values\")\n", " .orientation(\"vertical\")\n", " .side(\"right\")\n", " .finalize();" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "auto fig3 = xpl::figure_generator()\n", " .padding_x(0.025)\n", " .fig_margin(xeus::xjson({{\"top\", 50}, {\"bottom\", 70}, {\"left\", 50}, {\"right\", 100}}))\n", " .finalize();\n", "fig3.add_mark(scatter3);\n", "fig3.add_axis(ax_x);\n", "fig3.add_axis(ax_y);\n", "fig3.add_axis(ax2_c);\n", "fig3" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "ax2_c.tick_format = \"0.2f\";\n", "sc1.colors = std::vector{\"blue\", \"red\", \"green\", \"yellow\", \"orange\"};" ] }, { "cell_type": "markdown", "metadata": { "input_collapsed": false }, "source": [ "## Setting size and opacity based on data" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "std::size_t size_o = 100;\n", "std::vector x_data_o(size);\n", "std::iota(x_data_o.begin(), x_data_o.end(), 0);\n", "std::vector y_data_o = randn(size_o);\n", "std::vector c_data_o = randn(size_o);" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "xpl::linear_scale sx_o, sy_o;\n", "xpl::linear_scale sc_size_o, sc_opacities;" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "auto scatter_o = xpl::scatter_generator(sx_o, sy_o, sc_size_o, sc_opacities)\n", " .x(x_data_o)\n", " .y(y_data_o)\n", " .size(c_data_o)\n", " .stroke(\"black\")\n", " .default_size(128)\n", " .colors(std::vector>{\"orangered\"})\n", " .finalize();" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "xpl::axis ax_x_o(sx_o), ax_y_o(sy_o);\n", "ax_x_o.label = \"x\";\n", "ax_y_o.label = \"y\";\n", "ax_y_o.orientation = \"vertical\";\n", "ax_y_o.side = \"left\";" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "xpl::figure fig4;\n", "fig4.padding_x = 0.025;\n", "fig4.add_mark(scatter_o);\n", "fig4.add_axis(ax_x_o);\n", "fig4.add_axis(ax_y_o);\n", "fig4" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "// Changing the opacity of the scatter\n", "scatter_o.default_opacities = std::vector{0.5, 0.3, 0.1};" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "// Resetting the size for the scatter\n", "scatter_o.size = std::vector{};" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "// Resetting the opacity and setting the opacity according to the date\n", "scatter_o.default_opacities = std::vector{1.0};" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Scatter Chart Interactions" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Moving points in Scatter" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "std::size_t size_c = 10;\n", "std::vector x_data_c(size_c);\n", "std::iota(x_data_c.begin(), x_data_c.end(), 0);\n", "std::vector y_data_c = randn(size_c);" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "xpl::linear_scale sx_c, sy_c;" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "auto scatter_c = xpl::scatter_generator(sx_c, sy_c)\n", " .x(x_data_c)\n", " .y(y_data_c)\n", " .colors(std::vector>{\"orange\"})\n", " .enable_move(true)\n", " .finalize();" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "xpl::axis ax_x_c(sx_c), ax_y_c(sy_c);\n", "ax_x_c.label = \"x\";\n", "ax_y_c.label = \"y\";\n", "ax_y_c.orientation = \"vertical\";\n", "ax_y_c.side = \"left\";" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "xpl::figure fig5;\n", "fig5.padding_x = 0.025;\n", "fig5.add_mark(scatter_c);\n", "fig5.add_axis(ax_x_c);\n", "fig5.add_axis(ax_y_c);\n", "fig5" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "#include \"xwidgets/xlabel.hpp\"" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "xw::label label;\n", "label" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "inline void info(const ::xeus::xjson& content) {\n", " label.value = std::to_string(static_cast(content[\"point\"][\"x\"])) \n", " + \" \" \n", " + std::to_string(static_cast(content[\"point\"][\"y\"]));\n", "};" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "scatter_c.on_drag(info);\n", "scatter_c.on_drag_start(info);\n", "scatter_c.on_drag_end(info);" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "// Restricting movement to only along the Y-axis\n", "scatter_c.restrict_y = true;" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Adding points to Scatter" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "// TODO" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Updating X and Y while moving the point" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "// TODO" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Custom event on end of drag" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "// TODO" ] }, { "cell_type": "markdown", "metadata": { "collapsed": true }, "source": [ "## Adding tooltip and custom hover style" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "std::size_t size_tt = 50;\n", "std::vector x_data_tt(size_tt);\n", "std::iota(x_data_tt.begin(), x_data_tt.end(), 0);\n", "std::vector y_data_tt = randn(size_tt);" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "xpl::linear_scale sx_tt, sy_tt;" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "#include \"xplot/xdefault_tooltip.hpp\"\n", "xpl::tooltip def_tt;" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "def_tt.fields = std::vector>{\"$x^2$\", \"y\"};\n", "def_tt.formats = std::vector>{\"\", \".2f\"};" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "auto scatter_tt = xpl::scatter_generator(sx_tt, sy_tt)\n", " .x(x_data_tt)\n", " .y(y_data_tt)\n", " .colors(std::vector>{\"dodgerblue\"})\n", " .unhovered_style(::xeus::xjson::parse(R\"({\"opacity\": \"0.5\"})\"))\n", " .tooltip(def_tt)\n", " .finalize();" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "xpl::axis ax_x_tt(sx_tt), ax_y_tt(sy_tt);\n", "ax_x_tt.label = \"$x^2$\";\n", "ax_y_tt.label = \"y\";\n", "ax_y_tt.orientation = \"vertical\";\n", "ax_y_tt.side = \"left\";" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "xpl::figure fig6;\n", "fig6.padding_x = 0.025;\n", "fig6.add_mark(scatter_tt);\n", "fig6.add_axis(ax_x_tt);\n", "fig6.add_axis(ax_y_tt);\n", "fig6" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "// removing field names from the tooltip\n", "def_tt.show_labels = false;" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "// changing the fields displayed in the tooltip\n", "def_tt.fields = std::vector>{\"y\"};" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "ax_x_tt.label = \"x\";" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "anaconda-cloud": {}, "kernelspec": { "display_name": "C++14", "language": "C++14", "name": "xeus-cling-cpp14" }, "language_info": { "codemirror_mode": "text/x-c++src", "file_extension": ".cpp", "mimetype": "text/x-c++src", "name": "c++", "version": "14" } }, "nbformat": 4, "nbformat_minor": 1 }